Eon
Eon

Reputation: 3974

How do I populate a Html.DropDownListFor<> with the data from an entity model?

I am very new to to ASP.NET MVC 2, and I am having bucketloads of trouble to populate a dropdownlistfor<> with the data from a entity model. I suspect my LINQ may be wrong, but it doesn't hurt to find out why my code is not working.

I managed to pre-populate another DropDownListFor<> with the following code:

        <div class="editor-field">
            <% string[] Days = new string[] { "Monday", "Wednesday", "Friday" };%>
            <%--<%: Html.DropDownList("Delivery Day", new SelectList(Days)) %>--%>
            <%: Html.DropDownListFor(model => model.DeliveryDay, new SelectList(Days))%>
            <%: Html.ValidationMessageFor(model => model.DeliveryDay)%>
        </div>

The list I am trying to populate now looks like this:

        <div class="editor-field">
            <%  List<string> categoryList = new List<string>();
                var categories = from c in Model.Category select c.Category_Category;  
                foreach (object cata in categories)
                {
                    categoryList.Add(cata.ToString);
                }%>
            <%: Html.DropDownListFor(model => model.Category_Category,new SelectList(categoryList))%>
            <%: Html.ValidationMessageFor(model => model.Category)%>
        </div>

This is what happens:

Could not find an implementation of the query pattern for source type 'int?'. 'Select' not found.

How can I fix this up?

Upvotes: 1

Views: 1392

Answers (2)

kingpin
kingpin

Reputation: 1498

Use this for categories

<%  
    var categories = (from c in Model.Category select new SelectListItem 
        {
          Text = c.Category_Category.ToString(),
          Value = c.Category_Category.ToString() //or I will recommend using the key
        }).ToList();
%>    

and then

<%: Html.DropDownListFor(model => model.Category_Category, categories)%>

It will be better to do this in your controller and having separate view-model for your view containing List<SelectListItem> categories property.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

<div class="editor-field">
    <%  var categories = Model.Category.ToArray().Select(x => new SelectListItem
        {
            Value = x.CategoryId.ToString(),
            Text = x.CategoryName
        });
    %>
    <%= Html.DropDownListFor(model => model.Category_Category, categories) %>
    <%= Html.ValidationMessageFor(model => model.Category) %>
</div>

This being said I think you are missing the whole point of the MVC pattern. You should never be doing anything like that. A view is not supposed to be doing such things. You should not be passing your EF domain models to your view. You should be using view models and have your controller action do the necessary queries and prepare the view model.

So define a view model:

public class MyViewModel
{
    public string SelectedCategoryId { get; set; }
    public IEnumerable<SelectListItem> Categories { get; set; }
}

and then have your controller action build this view model:

public ActionResult Foo()
{
    var model = new MyViewModel();
    model.Categories = db.Category.ToArray().Select(x => new SelectListItem
    {
        Value = x.CategoryId.ToString(),
        Text = x.CategoryName
    });
    return View(model);
}

and then your view will be strongly typed to MyViewModel and you will be able to simply display the dropdownilst:

<div class="editor-field">
    <%= Html.DropDownListFor(x => x.SelectedCategoryId, Model.Categories) %>
    <%= Html.ValidationMessageFor(x => x.SelectedCategoryId) %>
</div>

Upvotes: 3

Related Questions