Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

javascript - ASP.NET MVC - Returning a PartialView to Ajax along with another object

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

Controller C#:

public ActionResult GetSomePartialView(SomeArgumentModel someArguments)
{

    return PartialView("_CaseManager");
}

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.

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:

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

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.

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).

Thanks in advance for any help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So - using the following posts I got this working:

Partial Views vs. Json (or both)

Render a view as a string

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...