JQuery,调用Action并返回View

时间:2011-10-17 14:26:45

标签: asp.net-mvc jquery-ui

我在JQuery中有一个有两个按钮的对话框。单击“上传任意”按钮时,我调用控制器的动作。     $(function(){     $( “#对话框:UI-对话”)。对话框( “破坏”);

$("#dialog-confirm").dialog({
    resizable: false,
    height: 300,
    width: 500,
    modal: true,
    buttons: {
        "Upload Anyway": function () {
            $(this).dialog("close");
            var month = '@ViewBag.duplicateString' ;
            var path = $("#path").val();
           $.getJSON('@Url.Action("UpdateComp")', { dateToUpdate: month, filePath: path }, 
            function()   {});
        },
        Cancel: function () {
            $(this).dialog("close");
        }
    }
});

在Controller中我只想处理数据并返回视图。

    public ActionResult UpdateComp (string dateToUpdate, string filePath)
    {
        //Process Data
        return View(compList.Values.AsEnumerable<CompUser>());
    }

当我点击“仍然上传”时,我已正确地重定向到Action(我已经通过调试检查),但视图未加载。我不是很擅长JQuery,所以也许我做错了。

感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

你在$ .getJSON上的回调什么也没做。请参阅http://api.jquery.com/jQuery.getJSON/并查看“成功”参数。

但是,如果你要返回一个视图(HTML等),那么你就不是“获取JSON”,而是获得了HTML。请尝试使用$.get$.post(取决于要求),并在成功回调中向用户显示返回的视图。

答案 1 :(得分:0)

将$ .getJSON更改$ .get并在我的主div中显示结果视图解决问题。

请注意,我渲染局部视图以便正确显示div。

这是JQuery代码,希望能帮助其他人。

$(function () {
$("#dialog:ui-dialog").dialog("destroy");
$("#dialog-confirm").dialog({
    resizable: false,
    height: 300,
    width: 500,
    modal: true,
    buttons: {
        "Upload Anyway": function () {
            $(this).dialog("close");
            var month = '@ViewBag.duplicateString' ;
            var path = $("#path").val();
            $.get('@Url.Action("UpdateComp")', { dateToUpdate: month, filePath: path }, function(result) {
                $('#main_div').html(result);
            })
                .error(function() { alert("A Problem occurs during the comparison"); });
        },
        Cancel: function () {
            $(this).dialog("close");
        }
    }
  });
});

答案 2 :(得分:0)

尝试一下

var url = '@Url.Action("method_name", "controller_name")?dateToUpdate=' + month + '&filePath=' + path;
window.location.href = url;
相关问题