In your opinion:
@Html.DropDownListFor(x => x.SiteId, new SelectList(Enumerable.Empty<SelectListItem>()))
where SiteId is the property of your view model that will receive the selected site identifier when submitting the form.
and then you can populate this dropdown with AJAX:
$(function() { $.getJSON('@Url.Action("GetSite", "Pedido")', function(result) { var ddl = $('#SiteId'); ddl.empty(); $(result).each(function() { ddl.append( $('<option/>', { value: this.Id }).html(this.Nome) ); }); }); });
and controller actions that return JSON data:
public ActionResult GetSite() { var sites = new[] { new { Id = "1", Nome = "site 1" }, new { Id = "2", Nome = "site 3" }, new { Id = "3", Nome = "site 3" }, }; return Json(sites, JsonRequestBehavior.AllowGet); }
Darin Dimitrov
source share