ASP.NET MVC - 将PartialView与另一个对象一起返回到Ajax

Mat*_*ttW 45 javascript jquery asp.net-mvc-3

我正在用ASP.NET MVC编写单页ajax应用程序 - 大量使用jQuery.我在整个应用程序中执行类似以下操作:

JS:

$.ajax({
    type: "GET",
    url: "/Home/GetSomePartialView/",
    data: someArguments,
    success: function (viewHTML) { 
        $("#someDiv").html(viewHTML); 
    },
    error: function (errorData) { onError(errorData); }
});
Run Code Online (Sandbox Code Playgroud)

控制器C#:

public ActionResult GetSomePartialView(SomeArgumentModel someArguments)
{

    return PartialView("_CaseManager");
}
Run Code Online (Sandbox Code Playgroud)

这非常有效.将viewHTML(在AJAX success功能)返回一个字符串,我可以推它在页面上没有问题.

现在我想做的是不仅返回PartialView HTML字符串,还返回某种状态指示器.这是权限的事情 - 例如,如果有人试图访问他们没有权限的应用程序的一部分,我想返回一个比他们要求的不同的PartialView,并在弹出窗口中显示一条消息告诉他们为什么他们有一个不同于他们所要求的观点.

所以 - 要做到这一点,我想做以下事情:

控制器C#:

public ActionResult GetSomePartialView(SomeArgumentModel someArguments)
{
    ReturnArgs r = new ReturnArgs();

    bool isAllowed = CheckPermissions(); 

    if (isAllowed) 
    {
        r.Status = 400; //good status ... proceed normally
        r.View = PartialView("_CaseManager");
    }
    else
    {
        r.Status = 300; //not good ... display permissions pop up
        r.View = PartialView("_DefaultView");
    }

    return Json(r);
}

public class ReturnArgs
{
    public ReturnArgs()
    {
    }

    public int Status { get; set; }
    public PartialViewResult View { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

JS:

$.ajax({
    type: "GET",
    url: "/Home/GetSomePartialView/",
    data: someArguments,
    success: function (jsReturnArgs) { 

        if (jsReturnArgs.Status === 300) { //300 is an arbitrary value I just made up right now
            showPopup("You do not have access to that.");
        }

        $("#someDiv").html(jsReturnArgs.View); //the HTML I returned from the controller
    },
    error: function (errorData) { onError(errorData); }
});
Run Code Online (Sandbox Code Playgroud)

这个SORTA现在正常工作.我在JavaScript中得到了一个好的对象(我期待看到的),但是我无法看到如何获取jsReturnArgs.View属性的完整HTML字符串.

我真的只是寻找相同的字符串,如果我只是单独返回它将返回PartialView.

(正如我在开头提到的,这是一个单页应用程序 - 所以我不能只将它们重定向到另一个视图).

在此先感谢您的帮助!

Mat*_*ttW 45

所以 - 使用以下帖子我得到了这个工作:

部分视图与Json(或两者)

将视图渲染为字符串

他们都很好地解决了,然后我将我的代码更改为以下内容:

C#:

public ActionResult GetSomePartialView(SomeArgumentModel someArguments)
{
    ReturnArgs r = new ReturnArgs();

    bool isAllowed = CheckPermissions(); 

    if (isAllowed) 
    {
        r.Status = 400; //good status ... proceed normally
        r.ViewString = this.RenderViewToString("_CaseManager");
    }
    else
    {
        r.Status = 300; //not good ... display permissions pop up
        r.ViewString = this.RenderViewToString("_DefaultView");
    }

    return Json(r);
}

public class ReturnArgs
{
    public ReturnArgs()
    {
    }

    public int Status { get; set; }
    public string ViewString { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

JS:

$.ajax({
    type: "GET",
    url: "/Home/GetSomePartialView/",
    data: someArguments,
    success: function (jsReturnArgs) { 

        if (jsReturnArgs.Status === 300) { //300 is an arbitrary value I just made up right now
            showPopup("You do not have access to that.");
        }

        $("#someDiv").html(jsReturnArgs.ViewString); //the HTML I returned from the controller
    },
    error: function (errorData) { onError(errorData); }
});
Run Code Online (Sandbox Code Playgroud)


Baz*_*nga 7

跳过必须返回带有多个参数的json并且你的html编码为json的一种方法是发送一个HTML,但你发送一个隐藏的字段,其中设置了状态或类似的东西..

success: function(data)
{
  if(data.find("#ajax-status").val()==="success")
  {
    $("#someDiv").html(data);
  }
  else
  {
    showPopup("You do not have access to that.");
  }
}
Run Code Online (Sandbox Code Playgroud)

我不推荐这个appraoch我会有两个部分视图一个用于普通视图,另一个用于错误/未经授权的情况..