ASP.NET MVC - 返回一个PartialView阿贾克斯与其他对象一起
我写一个单页的AJAX应用程序与ASP.NET MVC - 大量使用的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
(在阿贾克斯成功
功能)返回一个字符串,我可以推它的页面没有问题的
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:
他们都摊开来好听,后来我改变了我的code以下内容:
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); }
});