MVC4 Ajax-将完整模型传递给控制器

时间:2013-02-07 20:11:59

标签: ajax asp.net-mvc-4

我有我的AJAX电话

$.ajax({
    type: 'post',
    url: "/Store/LoadStoreIndex",
    data: , //Entire Model Here!!
    dataType: "text",
    success: function (result) {
        $('#Postback').html(result);
    }
});

我需要将整个模型发送回控制器,但经过多次搜索后找不到任何东西......有人能告诉我我需要做什么吗?

1 个答案:

答案 0 :(得分:4)

控制器获取行动

public ActionResult Index(YourModel model)
{
    YourModel model = new YourModel();

    return View(model);
}

查看

@model YourModel
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "form1" }))
{
    @Html.TextBoxFor(x => x.EmailAddress)
    @Html.TextBoxFor(x => x.Name)
    ...
}

脚本

$(function () {
    $('form').submit(function () {
        if ($(this).valid()) {
            $.ajax({
                url: this.action,
                type: this.method,
                // you can post your model with this line
                data: $(this).serialize(),  
                beforeSend: function () {

                },
                complete: function () {

                },
                success: function (result) {

                },
                error: function () {

                }
            });
        }
        return false;
    });
});

控制器发布操作

[HttpPost]
public ActionResult Index(YourModel model)
{
    return View();
}