Skip to content Skip to sidebar Skip to footer

Dropdownlistfor Without Foreach In Asp.net Mvc 3

ViewCode public IList Sites { get; set; } Controller (GetAll return a IList) newViewModel.Sites = siteRepository.GetAll(); View @Html.DropDownListFor ?

Solution 1:

It worked!

ViewModel

public IEnumerable<SelectListItem> Sites {get; set;}
publicstring SiteSelected {get; set;}

Controller

private IEnumerable<SelectListItem> GetAllSites()
{
    return context.SITE.Select(x => new SelectListItem
    {
        Text = x.NAME,
        Value = SqlFunctions.StringConvert((double)x.ID).Trim()
    }).ToList();
}

siteViewModel.Sites = GetAllSites();

View

@Html.DropDownListFor(model => model.SiteSelected , Model.Sites)

Solution 2:

See the accepted answer for this question

You need to modify it to appropriately work with an IList but it is a small code change.

Post a Comment for "Dropdownlistfor Without Foreach In Asp.net Mvc 3"