HTML5技术

ASP.NET MVC之下拉框绑定四种方式(十) - Recluse_Xpy(2)

字号+ 作者:H5之家 来源:H5之家 2016-04-24 14:00 我要评论( )

public class Employees{[Key] public int EmployeeId { get ; set ; }[Required, Display(Name = )] public string EmployeeName { get ; set ; }[Required, Display(Name = )] public String Address { get ; set

public class Employees { [Key] public int EmployeeId { get; set; } [Required, Display(Name = )] public string EmployeeName { get; set; } [Required, Display(Name = )] public String Address { get; set; } [Required, Display(Name = )] public int Province { get; set; } [Required, Display(Name = )] public int City { get; set; } [Display(Name = )] public String ZipCode { get; set; } [Required, Display(Name = )] public String Phone { get; set; } }

(2)初始化数据

List<Province> provinceList = new List<Province>() { ,Abbr=}, ,Abbr=}, ,Abbr=}, ,Abbr=} };

以及绑定ViewBag到下拉框和控制器上的方法:

[HttpGet] public ActionResult Create() { ViewBag.ProvinceList = provinceList; var model = new Employees(); return View(model); } [HttpPost] public ActionResult Create(Employees model) { if (ModelState.IsValid) { } ViewBag.ProvinceList = provinceList; return View(model); } public ActionResult FillCity(int provinceId) { var cities = new List<City>() { ,provinceId=1}, ,provinceId=2}, ,provinceId=3}, ,provinceId=4} }; cities = cities.Where(s => s.provinceId == provinceId).ToList(); return Json(cities, JsonRequestBehavior.AllowGet); }

 (3)视图展示

@using (Html.BeginForm()) { @Html.AntiForgeryToken() 注册雇员 @Html.LabelFor(m => m.EmployeeName, new { @class = "control-label col-md-2" }) @Html.TextBoxFor(m => m.EmployeeName, new { @class = "form-control" }) @Html.ValidationMessageFor(m => m.EmployeeName, "", new { @class = "text-danger" }) @Html.LabelFor(m => m.Address, new { @class = "control-label col-md-2" }) @Html.TextBoxFor(m => m.Address, new { @class = "form-control" }) @Html.ValidationMessageFor(m => m.Address, "", new { @class = "text-danger" }) @Html.LabelFor(m => m.Province, new { @class = "control-label col-md-2" }) @Html.DropDownListFor(m => m.Province, new SelectList(ViewBag.ProvinceList, "provinceId", "provinceName"), "选择所在省", new { @class = "form-control", @onchange = "FillCity()" }) @Html.ValidationMessageFor(m => m.Province, "", new { @class = "text-danger" }) @Html.LabelFor(m => m.City, new { @class = "control-label col-md-2" }) @Html.DropDownListFor(m => m.City, new SelectList(Enumerable.Empty(), "CityId", "CityName"), "选择所在市", new { @class = "form-control" }) @Html.ValidationMessageFor(m => m.City, "", new { @class = "text-danger" }) @Html.LabelFor(m => m.ZipCode, htmlAttributes: new { @class = "control-label col-md-2" }) @Html.TextBoxFor(m => m.ZipCode, new { @class = "form-control" }) @Html.ValidationMessageFor(m => m.ZipCode, "", new { @class = "text-danger" }) @Html.LabelFor(m => m.Phone, new { @class = "control-label col-md-2" }) @Html.TextBoxFor(m => m.Phone, new { @class = "form-control" }) @Html.ValidationMessageFor(m => m.Phone, "", new { @class = "text-danger" }) Cancel }

(4)根据省下拉框选择到市下拉框脚本

function FillCity() { var provinceId = $('#Province').val(); $.ajax({ url: '/Home/FillCity', type: "GET", dataType: "JSON", data: { provinceId: provinceId }, success: function (cities) { $("#City").html(""); $.each(cities, function (i, city) { $("#City").append( $('<option></option>').val(city.CityId).html(city.CityName)); }); } }); }

我们来看看整个过程:

 

结语

 对于下拉框绑定基本上已全部囊括进去,不断钻研,不断总结才能有能力上更好的提升。希望对阅读本文的你有所帮助,如果有帮助,不妨讨打(乞讨打赏)一下,1块也是爱:)。

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 如何在 ASP.NET Core 中发送邮件 - Savorboard

    如何在 ASP.NET Core 中发送邮件 - Savorboard

    2017-05-02 08:02

  • 十二个 ASP.NET Core 例子 - Savorboard

    十二个 ASP.NET Core 例子 - Savorboard

    2017-04-27 16:01

  • ASP.NET MVC5请求管道和生命周期 - 雪飞鸿

    ASP.NET MVC5请求管道和生命周期 - 雪飞鸿

    2017-04-24 08:04

  • ASP.NET Core MVC 源码学习:详解 Action 的激活 - Savorboard

    ASP.NET Core MVC 源码学习:详解 Action 的激活 - Savorboard

    2017-04-14 13:04

网友点评
(