0

我无法更新视图模型。

我有一个名为:概述

我在视图的控制器中有这个:

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

    return View(evo);
}

然后我保存按钮调用:

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

saveUrl 转到:

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

并且它在Save中运行良好,并且在saveData对象中的数据也很好,并且它直到最后都没有返回任何错误,但是当返回后它显示视图时,数据不再显示在那里。

请你帮助我好吗?

4

1 回答 1

2

A couple of remarks:

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

and your controller action:

[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);
}
于 2012-08-07T16:02:06.540 回答