MVC4 Ajax-通完整的模型控制器 [英] MVC4 Ajax- Pass complete model to controller

查看:116
本文介绍了MVC4 Ajax-通完整的模型控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的AJAX调用

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

我需要把我的整个模型反馈给控制器,但经过一番搜索找不到什么......谁能告诉我什么,我需要做?

I need to send my entire model back to the controller, but after much searching can't find anything ... Can somebody show me what I need to be doing?

推荐答案

控制器获取行动

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;
    });
});

控制器POST操作

Controller Post Action

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

这篇关于MVC4 Ajax-通完整的模型控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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