通过一个ActionResult返回空白页面中的多个视图

时间:2013-10-07 08:22:40

标签: jquery asp.net-mvc asp.net-mvc-3

如果有人可以提供建议,我会很感激。我有2个视图(非部分),我想要一次返回空白页面(target = "_blank")。 我的提交表单如下:

 @using (Html.BeginForm("Action1", "Controller1",  new { target = "_blank"})
{
//model data

<input type="submit" value = "Go" id="btn"/>
}

在我的控制器中,我有2个ActionResults:

public ActionResult Action1(MyModel model)
{
return View(model);
}

public ActionResult Action2(string year)
{
ViewBag.year = year;
return View();
}

所以我想同时打电话给Action1 and Action2Action1接受模型作为参数,Action2接受一些可以从MyModel获取的字符串。 有没有办法做到这一点? 我尝试通过jquery在提交按钮单击时调用Action2,但它无法正常工作,因为我想在新选项卡中显示视图。

提前致谢!

1 个答案:

答案 0 :(得分:0)

你应该能够做你想做的事。

最好创建一个新的动作,在单个JSON对象中的单独属性中返回两个视图 - 因为这会进行两次调用。

$("#Form").on("submit", function() {
    $.ajax({
        url: "/Controller/Action1",
        data: $("#Form").serialize(),
        success: function(response) {
            // do what you need to do with the response here
        }
    });

    $.ajax({
        url: "/Controller/Action2",
        data: { string : $("#Form").find("input#YOUR_INPUT_NAME").val() },
        success: function(response) {
            // do what you need to do with the response here
        }
    });
}

编辑:

假设target =“_ blank”正在运行,您应该能够创建第二个隐藏表单并在提交第一个表单时提交。

$("#Form").on("submit", function() {
    var i = $("#Form input#INPUT_ID_HERE").val()
    $("#Form2 input").val(i);
    $("#Form2").submit();        
}
相关问题