c# - Select Tag Helper in ASP.NET Core MVC -
i need select tag helper in asp.net core.
i have list of employees i'm trying bind select tag helper. employees in list<employee> employeeslist , selected value go employeeid property. view model looks this:
public class myviewmodel { public int employeeid { get; set; } public string comments { get; set; } public list<employee> employeeslist {get; set; } } my employee class looks this:
public class employee { public int id { get; set; } public string fullname { get; set; } } my question how tell select tag helper use id value while displaying fullname in drop down list?
<select asp-for="employeeid" asp-items="???" /> i'd appreciate this. thanks.
using select tag helpers render select element in view
in action, create object of view model, load employeelist collection property , send view.
public iactionresult create() { var vm = new myviewmodel(); vm.employeeslist = new list<employee> { new employee {id = 1, fullname = "shyju"}, new employee {id = 2, fullname = "scot"} }; return view(vm); } and in create view, create new selectlist object employeelist property , pass value asp-items property.
@model myviewmodel <form asp-controller="home" asp-action="create"> <select asp-for="employeeid" asp-items="@(new selectlist(model.employeeslist,"id","fullname"))"> <option>please select one</option> </select> <input type="submit"/> </form> and httppost action method accept submitted form data.
[httppost] public iactionresult create(myviewmodel model) { // check model.employeeid // : save , redirect } or if view model has list<selectlistitem> property dropdown items.
public class myviewmodel { public int employeeid { get; set; } public string comments { get; set; } public list<selectlistitem> employees { set; get; } } and in action,
public iactionresult create() { var vm = new myviewmodel(); vm.employees = new list<selectlistitem> { new selectlistitem {text = "shyju", value = "1"}, new selectlistitem {text = "sean", value = "2"} }; return view(vm); } and in view, can directly use employees property asp-items.
@model myviewmodel <form asp-controller="home" asp-action="create"> <label>comments</label> <input type="text" asp-for="comments"/> <label>lucky employee</label> <select asp-for="employeeid" asp-items="@model.employees" > <option>please select one</option> </select> <input type="submit"/> </form> the class selectlistitem belongs microsoft.aspnet.mvc.rendering namespace.
setting selected options
some times,you might want set 1 option default option in select element (for example, in edit screen, want load saved option value). that, may set employeeid property value value of option want selected.
public iactionresult create() { var vm = new myviewmodel(); vm.employees = new list<selectlistitem> { new selectlistitem {text = "shyju", value = "11"}, new selectlistitem {text = "tom", value = "12"}, new selectlistitem {text = "jerry", value = "13"} }; vm.employeeid =12; // here set value return view(vm); } this select option tom in select element when page rendered.
multi select dropdown
if want render multi select dropdown, can change view model property use asp-for attribute in view array type.
public class myviewmodel { public int[] employeeids { get; set; } public list<selectlistitem> employees { set; get; } } this render html markup select element multiple attribute allow user select multiple options.
@model myviewmodel <select id="employeeids" multiple="multiple" name="employeeids"> <option>please select one</option> <option value="1">shyju</option> <option value="2">sean</option> </select> setting selected options in multi select
similar single select, set employeeids property value array of values want.
public iactionresult create() { var vm = new myviewmodel(); vm.employees = new list<selectlistitem> { new selectlistitem {text = "shyju", value = "11"}, new selectlistitem {text = "tom", value = "12"}, new selectlistitem {text = "jerry", value = "13"} }; vm.employeeids= new int[] { 12,13} ; return view(vm); } this select option tom , jerry in multi select element when page rendered.
using viewbag transfer list of items
if not prefer keep collection type property pass list of options view, can use dynamic viewbag so.(this not recommended approach viewbag dynamic , code prone uncatched typo errors)
public iactionresult create() { viewbag.employees = new list<selectlistitem> { new selectlistitem {text = "shyju", value = "1"}, new selectlistitem {text = "sean", value = "2"} }; return view(new myviewmodel()); } and in view
<select asp-for="employeeid" asp-items="@viewbag.employees"> <option>please select one</option> </select>
Comments
Post a Comment