Javascript ASP.NET MVC - 将 PartialView 与另一个对象一起返回给 Ajax
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10589787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC - Returning a PartialView to Ajax along with another object
提问by MattW
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:
我正在使用 ASP.NET MVC 编写单页 ajax 应用程序 - 大量使用 jQuery。我在整个应用程序中执行了类似于以下操作:
JS:
JS:
$.ajax({
type: "GET",
url: "/Home/GetSomePartialView/",
data: someArguments,
success: function (viewHTML) {
$("#someDiv").html(viewHTML);
},
error: function (errorData) { onError(errorData); }
});
Controller C#:
控制器 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.
这很好用。将viewHTML
(在AJAXsuccess
功能)返回一个字符串,我可以推它在页面上没有问题。
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.
现在我想做的是不仅返回 PartialView HTML 字符串,而且还返回某种状态指示器。这是一个权限问题 - 例如,如果有人试图访问他们无权访问的应用程序的一部分,我想返回与他们要求的不同的 PartialView,并在弹出窗口中显示一条消息告诉他们为什么他们得到的视图与他们要求的不同。
So - to do this, I would like to do the following:
所以 - 要做到这一点,我想做以下事情:
Controller C#:
控制器 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:
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 SORTAworks 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.
这个SORTA现在有效。我在 JavaScript 中得到了一个好的对象(我期望看到的),但是我看不到如何获取该jsReturnArgs.View
属性的完整 HTML 字符串。
I am really just looking for the same string that would be returned if I were just returning the PartialView
by itself.
我真的只是在寻找相同的字符串,如果我只是返回PartialView
它本身,就会返回相同的字符串。
(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!
在此先感谢您的帮助!
采纳答案by MattW
So - using the following posts I got this working:
所以 - 使用以下帖子我得到了这个工作:
Partial Views vs. Json (or both)
They both lay it out nicely, then I changed my code to the following:
他们都很好地布置了它,然后我将代码更改为以下内容:
C#:
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:
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); }
});
回答by Baz1nga
one way to skip having to return a json with multiple parameters and your html encoded as json is to send an HTML always but you send a hidden field that has the status set in it or something like that..
跳过必须返回具有多个参数的 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.");
}
}
I wouldnt recommend this appraoch I would have two partial views one for the normal view and the other for the error/unauthorized case..
我不会推荐这种方法,我会有两个部分视图,一个用于正常视图,另一个用于错误/未经授权的情况。