-1

Having just started out on MVC I've tried numerous tutorials/forum posts but still can't get my DropDownList to be populated. I've managed to display the pub names in the form of ActionLinks in the view (so the correct data is being held in my Model right?), but have been unsuccessful with DropDownLists.

The error I recieve on the first DropDownList attempt reads:

"There is no ViewData item of type 'IEnumerable' that has the key 'PubId'."

Model:

namespace WineMenu.Models
{  
    public class PubListModel
    {       
        public List<Pub> _pl = new List<Pub>();

        public List<Pub> PubList
        {
            get
            {
                if (_pl.Count == 0)
                {
                    _pl = GetPubs();
                }

                return _pl;
            }
        }

    public List<Pub> GetPubs()
    {
            List<Pub> _pl = new List<Pub>();

            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=XXX\\SQLEXPRESS;Initial Catalog=DB_WineMenu;Integrated Security=True";
            con.Open();

            using (con)
            {
                SqlCommand cmd = new SqlCommand("SELECT * FROM Pub", con);
                SqlDataReader rd = cmd.ExecuteReader();

                while (rd.Read())
                {
                    Pub p = new Pub();

                    p.PubId = Convert.ToInt32(rd.GetInt32(0));
                    p.PubName = Convert.ToString(rd.GetSqlValue(1));

                    _pl.Add(p);
                }
            }
            return _pl;
    } 
    }

    public class Pub
    {
        public int PubId { get; set; }
        public string PubName { get; set; }
    }

Controller:

    public ActionResult GetPubs()
        {
            PubListModel p = new PubListModel();

            return View("GetPubs", p);
        }

View:

@model WineMenu.Models.PubListModel

@{
    ViewBag.Title = "GetPubs";
}

<h2>GetPubs</h2>

@*<h2>Pubs- Last Updated: @Model.PubName</h2>*@

<h4>@ViewBag.Message</h4>
<ul>
    @foreach (var pub in Model.PubList)
    {
        <li>@Html.ActionLink(pub.PubName, "Browse", new { pub = pub.PubName })</li>

        @Html.DropDownList("PubId", pub.PubName)
        @* @Html.DropDownListFor(p => p.PubList, Model.PubList)*@
    }

</ul>
Nick
  • 37
  • 1
  • 1
  • 7
  • Well yeah you are trying to create a drop downlist against the Pub class. This is exactly what I would except it to respond with. What are you *trying* to do here? – asawyer May 12 '16 at 18:42
  • Thanks for your reply. I'm trying to populate the DropDownList with all of the pubs in my DB. The Model has a list property that gets all of the pubs in the DB through the GetPubs() method. The Controller instantiates the Model, and I'm passing it to the GetPubs View. I can see that the Model has been filled through the debugger. Hopefully this helps? – Nick May 12 '16 at 21:12
  • Your model does not even have a property to bind to (i.e. for the selected `Pub`). Your model needs 2 properties - `int SelectedPub` and `IEnumerable` PubList` and in the view its `@Html.DropDownListFor(m => m.SelectedPub, Model.PubList)`. Suggest you read [this question/answer](http://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) to understand the basics. –  May 12 '16 at 22:40

1 Answers1

0

try this :

@Html.DropDownListFor(m => m.PubId, new SelectList(Model.PubListModel , "PubId", "PubName "), "--Select--", new { @class = "form-control" })
Mohammad Imran
  • 125
  • 1
  • 6