怎么办模型绑定使用jQuery阿贾克斯 [英] How to do Model Binding with Jquery Ajax

查看:190
本文介绍了怎么办模型绑定使用jQuery阿贾克斯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用模型绑定来保持我的控制器寻找更清洁,你可以看到它是如何更漂亮使用模型绑定:

I'd like to use model binding to keep my controllers looking cleaner, you can see how much nicer it is using model binding:

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

VS

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()

When doing model binding, we can just use a form with the correct names in the Html.TextBox("")

怎么样的jQuery?我怎样才能确保当我做了 $。员额(网址,数据,回调,数据类型)的 $。阿贾克斯(选项)致电创建(人personToCreate)的Person对象获取正确填写?

What about jquery? How can I make sure that when I do a $.post(url, data, callback, dataType) or a $.ajax(options) call to Create(Person personToCreate) that the Person object gets filled properly?

推荐答案

您需要:

  1. 请在数据的属性名相匹配的约束类型名称参数/属性。
  2. 始终为所有非空的参数/绑定类型的属性提供一个值。

2号是大一的,为什么结合的人可以表现不同于指定单个操作的参数为每个属性方面。如果你有一个类型有一个名为富非空的属性,则不能提供一个Foo项表单将prevent结合。

Number 2 is the big one in terms of why binding to person can behave differently than specifying individual action arguments for each property. If you have a type with a non-nullable property called "Foo", then failing to supply a foo item in your form will prevent binding.

这篇关于怎么办模型绑定使用jQuery阿贾克斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆