本文介绍了MVC模型绑定通过AJAX请求不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一个小麻烦通过AJAX MVC模型绑定。
Having a little trouble with MVC model binding via AJAX.
谁能告诉我,为什么CreateTransfereeDetails属性没有约束力,但它总是回来为零。
Can someone tell me why the CreateTransfereeDetails property is not binding, it always comes back as 'null'.
型号:
public class ResolveProfileSelectionRequiredModel
{
public CreateTransfereeModel CreateTransfereeDetails { get; set; }
public bool NewTransfereeSelected { get; set; }
}
public class CreateTransfereeModel
{
[Display(Name = "Transferee Name:")]
public string TransfereeName { get; set; }
}
HTML:
Html:
<input type="text" id="TransfereeName" />
<input type="hidden" id="NewTrasnfereeSelected" />
JavaScript的:
JavaScript:
var createTransfereeDetails =
{
"TransfereeName": $("#TransfereeName").val()
};
$.ajax({
url: "/myurl",
dataType: "json",
traditional: true,
type: "POST",
data: {
CreateTransfereeDetails: createTransfereeDetails,
NewTransfereeSelected: $("#NewTransfereeSelected").val()
},
success: function (result) {
//
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
//
},
complete: function () {
//
}
});
谢谢!
推荐答案
使用的name属性
在内部表单输入字段。 名称属性值
被自动分配到模型的属性。
use name attribute
on input fields inside form. Name attribute's values
are automatically assigned to model's properties.
<form method="post" id="frm">
<input type="text" name="id="TransfereeName" " id="TransfereeName" />
<input type="hidden" name="NewTrasnfereeSelected" id="NewTrasnfereeSelected" />
<input type="button" onclick="submit()" value="submit" />
</form>
和使用序列化()jQuery的传递用ajax数据的功能
and use serialize() function of jquery to pass data using ajax
function submit(){
$.ajax({
url: "/myurl",
dataType: "json",
traditional: true,
type: "POST",
data: $('#frm').serialize(),
success: function (result) {
//
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
//
},
complete: function () {
//
}
});
}
这篇关于MVC模型绑定通过AJAX请求不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!