How to bind enumeration values in the mvc drop-down box and the mvc drop-down box to Enumeration
In general BS program development, I am used to using the asp.net control to directly bind data to the background. However, I recently encountered a problem. In mvc, how can I bind background data to the foreground?
Previously, we listed data directly on the front-end page as follows:
<SOA: HBDropDownList ID = "ddlReadState" runat = "server" Width = "200px"> <asp: listItem Text = "select" Value = "-1"> </asp: ListItem> <asp: ListItem Text = "completed" Value = "1"> </asp: listItem> <asp: ListItem Text = "unfinished, known" Value = "0"> </asp: ListItem> </SOA: HBDropDownList>
Alternatively, bind the data source directly in the background using the drop-down box Control ID.
this.ddlReadState.DataSource = List<State>;
However, in mvc, the controller cannot directly use the control ID to bind data. It can only bind data at the front-end. How can we update data?
Model Code:
Public class SignQuoteFormViewModel: WfDataViewModelBase <SignQuoteForm> {public string ReadState {get; set;} public string ReadRemark {get; set;} public List <SelectListItem> GetSelectList () {List <SelectListItem> list = new List <SelectListItem> {new SelectListItem {Text = "select", Value = "-1 "}, new SelectListItem {Text = "completed", Value = "0"}, new SelectListItem {Text = "unfinished, please know", Value = "1 "}}; return list; }} public enum selectListStatus {// <summary> // completed /// </summary> [EnumItemDescription ("completed")] NoFinish = 0, /// <summary> /// incomplete /// </summary> [EnumItemDescription ("incomplete")] Finished = 1}
Front-end data binding:
<div style="padding:15px;border-radius:0;" class="panel-body"> @Html.ViewComponent().DropDownList(Html, m => m.ReadState, Model.GetSelectList(), new { @class = "form-control" }) </div>
As follows: