ASP.NET MVC - 将部分视图与另一个对象一起返回到Ajax
问题描述:
我正在用ASP.NET MVC编写单页ajax应用程序 - 大量使用jQuery。我做一些类似以下内容的整个应用程序:ASP.NET MVC - 将部分视图与另一个对象一起返回到Ajax
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
函数中)作为字符串返回,我可以将它推到页面上没有问题。
现在我想要做的是不仅返回部分视图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; }
}
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字符串。
我真的只是寻找相同的字符串,如果我只是自己返回PartialView
就会返回。
(正如我在开始时提到的,这是一个单页面应用程序 - 因此我不能将它们重定向到另一个视图)。
在此先感谢您的帮助!
答
所以 - 使用下面的帖子中,我得到了这个工作:
Partial Views vs. Json (or both)
他们都摊开来好听,于是我改变了我的代码如下:
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); }
});
答
一个办法跳过不必返回多个参数的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.");
}
}
我wouldnt推荐这个appraoch我会有两个部分视图一个为正常的观点,另一个为错误/未授权的情况..
我很欣赏的投入!如果可以的话,我试图避免这样的事情。 – MattW