c# - How to turn a Selectlist into Html.SelectListFor() -
i need following turned @html helper drop down list.
<select size="30" class="scrollableinside" id="customerselect"> @foreach (var customer in model.customers) { <option value=@customer.customercontacts.first().customercontactid>@customer.customername 	 	 @customer.customercontacts.first().phone</option> } </select>
since i'm doing little unnorthodox .first() , 2 pieces of data being put list i'm not sure how make @html.selectlistfor this.
@html.dropdownlistfor(model => model.customerid, new selectlist(model.customers, "customerid", "what put here"))
you should try this
public class indexviewmodel { // stores selected value drop down box. [required] public int countryid { get; set; } // contains list of countries. public selectlist countries { get; set; } } public class homecontroller : controller { [httpget] public actionresult index() { indexviewmodel viewmodel = new indexviewmodel(); viewmodel.countries = new selectlist(getcountries(), "id", "name"); return view(viewmodel); } [httppost] public actionresult index(indexviewmodel viewmodel) { viewmodel.countries = new selectlist(getcountries(), "id", "name"); if (!modelstate.isvalid) return view(viewmodel); //todo: selected country... cmsservice.updatecurrentlocation(viewmodel.countryid); return view(viewmodel); } }
in view
@html.dropdownlistfor(x => x.countryid, model.countries)
or can use
@html.dropdownlistfor(x => x.countryid, model.countries, "- please select -")
Comments
Post a Comment