MVC3接收到新的模型数据后提交 [英] MVC3 receiving the new model data after submit

查看:98
本文介绍了MVC3接收到新的模型数据后提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能更新视图模型。

我认为叫:概述

我有这样的视图控制器:

I have this in the view's controller:

public ActionResult Overview()
{
    var evo = new OverviewViewObject
    {
        MvcGridModel = new MvcGrid(),  
        SingleExportData = new SingleExportData()
    };

    return View(evo);
}

然后,我有保存
按钮卡列斯:

Then i have Save The buttons calles:

$.ajax({
         url: saveUrl,
         cache: false,
         type: "post",
         data: JSON.stringify({  Name:  myName  }),
          contentType: "application/json",
          success: function (data) { .. }...

在saveUrl去:

the saveUrl goes to:

[HttpPost]
public ActionResult Save(MyDataType saveData)
{
    //todo save logic here

    var mvcGridModel =  GetGridData();
    var evo = new ExportDataOverviewViewObject
    {
        MvcGridModel = mvcGridModel ?? new MvcGrid(),
        SaveData = new MyDataType()
    };

    return View("Overview", evo);
}

和它在保存变为细,而且它得到精在SAVEDATA对象中的数据,并且它好好尝试返回任何错误,直到结束,但是当返回后它显示的视图中,没有显示在那里的数据了。

And it goes fine in the Save, and it get fine the data in the saveData object, and it doens't return any error till the end, but when after the return it shows the view,the data is not displayed there anymore.

您能帮我吗?

推荐答案

一对夫妇的言论:


  • 如果保存按钮是一个表单提交按钮或锚确保您从$就调用回调后返回false,以确保不被执行的默认操作

  • 在您的控制器操作返回的是一个完整的视图(返回查看()),而不是它是更常见的是正在调用的控制器操作的局部视图使用AJAX。

  • If the Save button is a form submit button or an anchor make sure that you return false from the callback after the $.ajax call to ensure that the default action is not executed
  • In your controller action you are returning a full view (return View()) instead of a partial view which is what is more common for controller actions that are being invoked with AJAX.

因此​​,要回顾一下:

So to recap:

$('#saveButton').click(function() {
    $.ajax({
        url: saveUrl,
        cache: false,
        type: 'POST',
        data: JSON.stringify({ Name: myName }),
        contentType: 'application/json',
        success: function (data) { 
            // do something with the data => refresh some
            // portion of your DOM
            $('#someDivId').html(data);
        }
    });
    return false;
});

和您的控制器动作:

[HttpPost]
public ActionResult Save(MyDataType saveData)
{        
    //todo save logic here

    var mvcGridModel = GetGridData();
    var evo = new ExportDataOverviewViewObject
    {
        MvcGridModel = mvcGridModel ?? new MvcGrid(),
        SaveData = new MyDataType()
    };
    return PartialView("Overview", evo);
}

这篇关于MVC3接收到新的模型数据后提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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