2

通过 AJAX 进行 MVC 模型绑定有点麻烦。

有人可以告诉我为什么 CreateTransfereeDetails 属性没有绑定,它总是返回为“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:

<input type="text" id="TransfereeName" />
<input type="hidden" id="NewTrasnfereeSelected" />

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 () {
        //
    }
});

谢谢!

4

1 回答 1

2

用于name attribute表单内的输入字段。Name attribute's values自动分配给模型的属性。

<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 的 serialize() 函数使用 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 () {
            //
        }
    });
}
于 2013-10-01T17:48:21.320 回答