使用 RenderPartialToString 时显示模型状态错误的问题
我使用以下代码:
public string RenderPartialToString(ControllerContext context, string partialViewName, ViewDataDictionary viewData, TempDataDictionary tempData)
{
ViewEngineResult result = ViewEngines.Engines.FindPartialView(context, partialViewName);
if (result.View != null)
{
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
{
using (HtmlTextWriter output = new HtmlTextWriter(sw))
{
ViewContext viewContext = new ViewContext(context, result.View, viewData, tempData, output);
result.View.Render(viewContext, output);
}
}
return sb.ToString();
}
return String.Empty;
}
通过 JSON 返回部分视图和表单。它按预期工作,但是一旦我收到模型状态错误,我的 ValidationSummary 就不会显示。 JSON 仅返回默认形式,但不会突出显示验证错误或显示验证摘要。
我错过了什么吗?
这就是我调用 RenderPartialToString 的方式:
string partialView = RenderPartialToString(this.ControllerContext, "~/Areas/User/Views/Account/ChangeAccountDetails.ascx", new ViewDataDictionary(avd), new TempDataDictionary());
Im using the following code:
public string RenderPartialToString(ControllerContext context, string partialViewName, ViewDataDictionary viewData, TempDataDictionary tempData)
{
ViewEngineResult result = ViewEngines.Engines.FindPartialView(context, partialViewName);
if (result.View != null)
{
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
{
using (HtmlTextWriter output = new HtmlTextWriter(sw))
{
ViewContext viewContext = new ViewContext(context, result.View, viewData, tempData, output);
result.View.Render(viewContext, output);
}
}
return sb.ToString();
}
return String.Empty;
}
To return a partial view and a form through JSON. It works as it should, but as soon as I get modelstate errors my ValidationSummary does not show. The JSON only return the default form but it does not highlight the validation errors or show the validation summary.
Am I missing something?
This is how I call the RenderPartialToString:
string partialView = RenderPartialToString(this.ControllerContext, "~/Areas/User/Views/Account/ChangeAccountDetails.ascx", new ViewDataDictionary(avd), new TempDataDictionary());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在这个问题上花费了太多时间后,我发现
ModelState
项不应该添加到viewContext.Controller.ViewData.ModelState
中,而应该添加到>viewContext.ViewData.ModelState
。进行此更改后,将呈现ModelState
错误。After spending too much time on this problem I saw that the
ModelState
items shouldn't be added to theviewContext.Controller.ViewData.ModelState
, it should be added toviewContext.ViewData.ModelState
. After making this change theModelState
errors were being rendered.我对类似的代码遇到了同样的问题:
添加此行后全部修复:
如果我对这个特定场景进行移植,它将类似于
I had the same issue with a similar code:
All fix when added this lines:
If i do a port to this particular scenario it ll be something like
这就是我为使其工作而所做的:
不确定为什么我需要 { } 的..
This is what I did to make it work:
Not sure why I needed the { } 's..
我对上述评论中提出的所有解决方案都存在一些问题,因此我对其进行了一些改进,以确保它在所有可能的情况下都能正常工作:
I had some problems with all of the solutions presented in the above comments, so I refined it a bit just to be sure that it works appropriately in all possible cases: