5

我想使用模型绑定来让我的控制器看起来更干净,你可以看到使用模型绑定有多好:

public ActionResult Create(Person personToCreate)
{
    //Create person here
}

对比

public ActionResult Create(string firstName, string lastName, string address, string phoneNum, string email, string postalCode, string city, string province, string country)
{
    //Create person here
}

在进行模型绑定时,我们可以在Html.TextBox("")

jQuery呢?如何确保当我执行$.post(url, data, callback, dataType)$.ajax(options)调用Create(Person personToCreate)Person 对象时正确填充?

4

1 回答 1

4

你需要:

  1. 使数据中的属性名称与绑定类型名称的参数/属性匹配。
  2. 始终为绑定类型的所有不可为空的参数/属性提供一个值。

就为什么绑定到人的行为可能不同于为每个属性指定单独的操作参数而言,第 2 点是一个重要的问题。如果您的类型具有名为“Foo”的不可为空的属性,则未能在表单中提供 foo 项将阻止绑定。

于 2009-07-13T20:42:06.943 回答