Asp.Net MVC模型绑定
Asp.Net MVC内置了两种不同的模型绑定功能,第一种是使用UpdateModel()更新一个已存在的模型对象的属性,第二种是传递模型对象,作为action方法的参数。这两种绑定的功能都相当强大。如下:
第一种模型绑定
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection formValues)
{
Dinners dinners = dinnerRepository.GetDinners(id);
try
{
UpdateModel(dinners);
dinnerRepository.Save();
return RedirectToAction("Details", new { id = dinners.DinnerID });
}
catch
{
ModelState.AddRuleViolations(dinners.GetRuleViolations());
return View(dinners);
}
}
public ActionResult Edit(int id, FormCollection formValues)
{
Dinners dinners = dinnerRepository.GetDinners(id);
try
{
UpdateModel(dinners);
dinnerRepository.Save();
return RedirectToAction("Details", new { id = dinners.DinnerID });
}
catch
{
ModelState.AddRuleViolations(dinners.GetRuleViolations());
return View(dinners);
}
}
第二种模型绑定
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Dinners dinners)
{
if (ModelState.IsValid)
{
try
{
dinners.HostedBy = "SomeUser";
dinnerRepository.Add(dinners);
dinnerRepository.Save();
return RedirectToAction("Details", new { id = dinners.DinnerID });
}
catch
{
ModelState.AddRuleViolations(dinners.GetRuleViolations());
}
}
return View(dinners);
}
public ActionResult Create(Dinners dinners)
{
if (ModelState.IsValid)
{
try
{
dinners.HostedBy = "SomeUser";
dinnerRepository.Add(dinners);
dinnerRepository.Save();
return RedirectToAction("Details", new { id = dinners.DinnerID });
}
catch
{
ModelState.AddRuleViolations(dinners.GetRuleViolations());
}
}
return View(dinners);
}
默认情况下,UpdateModel()方法试图根据匹配的表单参数值,更新所有的属性。同样的作为参数传递给action方法的模型对象,基于表单参数设置全部模型对象的属性。但是有时,我们根据显式的指定模型对象的部分属性可更新,此种做法称作模型绑定的锁定,可分为基于用途来锁定绑定和基于类型来锁定绑定两种方式。
基于用途来锁定绑定,下面是针对第一种模型绑定的锁定方式:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection formValues)
{
Dinners dinners = dinnerRepository.GetDinners(id);
//指定模型对象可更新的属性列表
String[] allowPropery = new[] { "Title", "Description", "ContactPhone" };
try
{
//使用UpdateModel重载方法
UpdateModel(dinners,allowPropery);
dinnerRepository.Save();
return RedirectToAction("Details", new { id = dinners.DinnerID });
}
catch
{
ModelState.AddRuleViolations(dinners.GetRuleViolations());
return View(dinners);
}
}
public ActionResult Edit(int id, FormCollection formValues)
{
Dinners dinners = dinnerRepository.GetDinners(id);
//指定模型对象可更新的属性列表
String[] allowPropery = new[] { "Title", "Description", "ContactPhone" };
try
{
//使用UpdateModel重载方法
UpdateModel(dinners,allowPropery);
dinnerRepository.Save();
return RedirectToAction("Details", new { id = dinners.DinnerID });
}
catch
{
ModelState.AddRuleViolations(dinners.GetRuleViolations());
return View(dinners);
}
}
第二种模型绑定的锁定方式:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Include="Title,Description,ContactPhone")]Dinners dinners)
{
if (ModelState.IsValid)
{
try
{
dinners.HostedBy = "SomeUser";
dinnerRepository.Add(dinners);
dinnerRepository.Save();
return RedirectToAction("Details", new { id = dinners.DinnerID });
}
catch
{
ModelState.AddRuleViolations(dinners.GetRuleViolations());
}
}
return View(dinners);
}
public ActionResult Create([Bind(Include="Title,Description,ContactPhone")]Dinners dinners)
{
if (ModelState.IsValid)
{
try
{
dinners.HostedBy = "SomeUser";
dinnerRepository.Add(dinners);
dinnerRepository.Save();
return RedirectToAction("Details", new { id = dinners.DinnerID });
}
catch
{
ModelState.AddRuleViolations(dinners.GetRuleViolations());
}
}
return View(dinners);
}
基于类型来锁定绑定
基于类型来说锁定绑定,这样一旦制定绑定规则,就可以在所有Controller和Action中使用,包括UpdateModel方法和Action方法的参数。
基于类型来说锁定绑定,这样一旦制定绑定规则,就可以在所有Controller和Action中使用,包括UpdateModel方法和Action方法的参数。
通过添加[Bind]属性到类上,或者在应用程序中的Global.asax文件中(如果类型不是我们自定义的情况下,非常有用),来定制类型绑定规则。使用Bind属性的Include和Exclude属性来指定类或接口中的那些属性是可以绑定的。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
namespace NerdDinner.Models
{
[System.Web.Mvc.Bind(Include="Title,Description,ContactPhone")]
public partial class Dinners
{
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
namespace NerdDinner.Models
{
[System.Web.Mvc.Bind(Include="Title,Description,ContactPhone")]
public partial class Dinners
{
}
}