Home  >  Article  >  Backend Development  >  Specific code implementation of MVC5 drop-down box radio selection binding

Specific code implementation of MVC5 drop-down box radio selection binding

零下一度
零下一度Original
2017-07-02 10:30:222281browse

This article mainly introduces MVC5 drop-down box binding in detail, which has certain reference value. Interested friends can refer to it

The example of this article shares the MVC5 drop-down box radio selection with everyone. The specific binding code is for your reference. The specific content is as follows

1.Model


[Display(Name = "学历")]
 public ICollection<System.Web.Mvc.SelectListItem> asdflist{ get; set; }  //下拉框的类型

[Display(Name = "学历")]
[Required]
public int asdf { get; set; }    //学历这个字段的属性

2.controller

(1) First write a program binding, which can be through database binding or direct binding


[Description("学历")]
[LoginAllowView]
 private List<SelectListItem> bind_Education()
{
     StringBuilder sb = new StringBuilder();
     sb.Append(" select id,name ");
     sb.Append(" from Edu_file ");
     DataTable dt = sqlHelp.getData(sb.ToString());//sqlHelp是已经写好的帮助类,便于数据库的操作
     var factorOptions = dt.AsEnumerable().Select(row => new SelectListItem
      {
        Text = row["name"],
        Value = row["id"]
      }).ToList();
      return factorOptions;
}

[Description("学历")]
[LoginAllowView]
private List<SelectListItem> bind_Education()
{
    List<SelectListItem> listItem = new List<SelectListItem>();
    listItem.Add(new SelectListItem { Text = "本科", Value = "1" });
    listItem.Add(new SelectListItem { Text = "硕士", Value = "2" });
     listItem.Add(new SelectListItem { Text = "博士", Value = "3" });
     return listItem;
 }

(2) Initialize, and Passed to view


[Description("我的学历")]
[UIExceptionResult]
 public ActionResult Edu()
{
    var edu= new EduModel();
    edu.asdflist=bind_Education();  //初始化下拉框的值
    return View(edu);
 }

3. View


@model RsJob.Web.Models.EduModel  
<p class="form-group">
    @Html.LabelFor(m => m.agj03, new { @class = "col-sm-2 control-label" })
        <p class="col-sm-10">
          @Html.DropDownListFor(model => model.asdf, Model.asdflist, new { @class = "form-control select2", style = "width: 100%;" })
          @Html.ValidationMessageFor(m => m.asdf, "", new { @class = "text-danger" })
        </p>
 </p>

select2 is bootstrap style, js add: $('.select2').select2();

The above is the detailed content of Specific code implementation of MVC5 drop-down box radio selection binding. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn