使用 RenderPartialToString 时显示模型状态错误的问题

发布于 2024-08-30 09:54:58 字数 1206 浏览 6 评论 0原文

我使用以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

∝单色的世界 2024-09-06 09:54:58

在这个问题上花费了太多时间后,我发现 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 the viewContext.Controller.ViewData.ModelState, it should be added to viewContext.ViewData.ModelState. After making this change the ModelState errors were being rendered.

掐死时间 2024-09-06 09:54:58

我对类似的代码遇到了同样的问题:

添加此行后全部修复:

// copy model state items to the html helper
                foreach (var item in context.Controller.ViewData.ModelState)
                    html.ViewData.ModelState.Add(item);

如果我对这个特定场景进行移植,它将类似于

    ViewContext viewContext = new ViewContext(context, result.View, viewData, tempData, output);

//Copy the ModelSate            
    foreach (var item in context.Controller.ViewData.ModelState)
    viewContext.Controller.ViewData.ModelState.Add(item);

result.View.Render(viewContext, output);

I had the same issue with a similar code:

All fix when added this lines:

// copy model state items to the html helper
                foreach (var item in context.Controller.ViewData.ModelState)
                    html.ViewData.ModelState.Add(item);

If i do a port to this particular scenario it ll be something like

    ViewContext viewContext = new ViewContext(context, result.View, viewData, tempData, output);

//Copy the ModelSate            
    foreach (var item in context.Controller.ViewData.ModelState)
    viewContext.Controller.ViewData.ModelState.Add(item);

result.View.Render(viewContext, output);
临风闻羌笛 2024-09-06 09:54:58

这就是我为使其工作而所做的:

foreach (var item in context.Controller.ViewData.ModelState)
{
    viewContext.Controller.ViewData.ModelState.Add(item);
}

不确定为什么我需要 { } 的..

This is what I did to make it work:

foreach (var item in context.Controller.ViewData.ModelState)
{
    viewContext.Controller.ViewData.ModelState.Add(item);
}

Not sure why I needed the { } 's..

想你只要分分秒秒 2024-09-06 09:54:58

我对上述评论中提出的所有解决方案都存在一些问题,因此我对其进行了一些改进,以确保它在所有可能的情况下都能正常工作:

foreach (var item in controllerContext.Controller.ViewData.ModelState)
{
    if (item.Value.Errors.Any())
    {
        viewContext.ViewData.ModelState.Add(item);    
    }
}

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:

foreach (var item in controllerContext.Controller.ViewData.ModelState)
{
    if (item.Value.Errors.Any())
    {
        viewContext.ViewData.ModelState.Add(item);    
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文