ASP.NET MVC - 将 PartialView 与另一个对象一起返回给 Ajax [英] ASP.NET MVC - Returning a PartialView to Ajax along with another object

查看:23
本文介绍了ASP.NET MVC - 将 PartialView 与另一个对象一起返回给 Ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing a single page ajax app with ASP.NET MVC - making heavy use of jQuery. I do something similar to the following throughout the app:

JS:

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

控制器 C#:

public ActionResult GetSomePartialView(SomeArgumentModel someArguments)
{

    return PartialView("_CaseManager");
}

这很好用.viewHTML(在 ajax success 函数中)作为字符串返回,我可以将它推到页面上没问题.

This works great. The viewHTML (in the ajax success function) is returned as a string and I can shove it on the page no problem.

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

Now what I would like to do is to return not only the PartialView HTML string, but also some sort of status indicator. This is a permissions thing - for instance, if someone tries to get to a portion of they app they don't have permission to, I want to return a different PartialView than they asked for and also display a message in a popup window telling them why they got an View different from what they asked for.

所以 - 为此,我想执行以下操作:

So - to do this, I would like to do the following:

控制器 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; }
}

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

这个 SORTA 现在可以使用了.我在 JavaScript 中得到了一个好的对象(我期望看到的),但是我看不到如何获取 jsReturnArgs.View 属性的完整 HTML 字符串.

This SORTA works right now. I get a good object in JavaScript (what I am expecting to see), however I cannot see how to get at the full HTML string of the jsReturnArgs.View property.

我真的只是在寻找如果我只是返回 PartialView 本身就会返回的相同字符串.

I am really just looking for the same string that would be returned if I were just returning the PartialView by itself.

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

(As I mentioned at the beginning, this is a single page app - so I can't just redirect them to another View).

在此先感谢您的帮助!

推荐答案

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

So - using the following posts I got this working:

部分视图与 Json(或两者)

将视图渲染为字符串

他们都很好地布局,然后我将代码更改为以下内容:

They both lay it out nicely, then I changed my code to the following:

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

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

这篇关于ASP.NET MVC - 将 PartialView 与另一个对象一起返回给 Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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