在按钮上单击我调用了一个js函数,该函数调用为action方法。但如果json结果为0(不是错误),我想重定向到局部视图。 JS功能:
function AssignButtonClicked(step, parent, show) {
alert("coming: " + step + " parent: " + parent + " show is : " + show);
$.ajax({
type: "POST",
url: "/Jobs/PassInstructionTest",
data: "{stepGuid: '" + step + "', parentGuid: '" + parent + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert("resp is : " + response);
if (response == '0') {
alert('qa called!');
$("#forqa").show();
}
if (response == '1') {
}
},
error: function (response) {
alert(response.responseText + " error for fail");
},
});
return false;
}
控制器中的操作方法:
public ActionResult PassInstructionTest(Guid stepGuid, Guid parentGuid, string show)
{
bool isQA = false;
if (!isQA)
{
return Json(0, JsonRequestBehavior.AllowGet);
}
else
{
return PartialView("MyPartialView");
}
}
当调用MyPartialView
时,它会抛出错误,因为执行子请求时出错。
请提供解决方案。
答案 0 :(得分:0)
将Html转换为字符串然后将其作为json传递。
public virtual string RenderPartialViewToString(string viewName, object model)
{
//Original source code: http://craftycodeblog.com/2010/05/15/asp-net-mvc-render-partial-view-to-string/
if (string.IsNullOrEmpty(viewName))
viewName = this.ControllerContext.RouteData.GetRequiredString("action");
this.ViewData.Model = model;
using (var sw = new StringWriter())
{
ViewEngineResult viewResult = System.Web.Mvc.ViewEngines.Engines.FindPartialView(this.ControllerContext, viewName);
var viewContext = new ViewContext(this.ControllerContext, viewResult.View, this.ViewData, this.TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
public ActionResult PassInstructionTest(Guid stepGuid, Guid parentGuid,string show)
{
bool isQA = false;
if (!isQA)
{
return Json(0, JsonRequestBehavior.AllowGet);
}
else
{
return update_section = new
{
ShippingMethodUpdateHtml = this.RenderPartialViewToString("MyPartialView", null)
},
}
}