提交Ajax表单后如何用消息更新_Layout.cshtml中的DIV?
目前我有一个像这样的 Razor View
:
TotalPaymentsByMonthYear.cshtml
@model MyApp.Web.ViewModels.MyViewModel
@using (@Ajax.BeginForm("TotalPaymentsByMonthYear",
new { reportName = "CreateTotalPaymentsByMonthYearChart" },
new AjaxOptions { UpdateTargetId = "chartimage"}))
{
<div class="report">
// MyViewModel fields and validation messages...
<input type="submit" value="Generate" />
</div>
}
<div id="chartimage">
@Html.Partial("ValidationSummary")
</div>
然后我显示一个 PartialView
,它有一个 @Html.ValidationSummary()
以防验证错误。
ReportController.cs
public PartialViewResult TotalPaymentsByMonthYear(MyViewModel model,
string reportName)
{
if (!ModelState.IsValid)
{
return PartialView("ValidationSummary", model);
}
model.ReportName = reportName;
return PartialView("Chart", model);
}
我想要做的是:我正在寻找一种将此验证错误消息发送到我在 _Layout.cshtml
文件中定义的 DIV 元素。
_Layout.cshtml
<div id="message">
</div>
@RenderBody()
我想异步填充此 DIV 的内容。这可能吗?我怎样才能做到这一点?
Currently I have a Razor View
like this:
TotalPaymentsByMonthYear.cshtml
@model MyApp.Web.ViewModels.MyViewModel
@using (@Ajax.BeginForm("TotalPaymentsByMonthYear",
new { reportName = "CreateTotalPaymentsByMonthYearChart" },
new AjaxOptions { UpdateTargetId = "chartimage"}))
{
<div class="report">
// MyViewModel fields and validation messages...
<input type="submit" value="Generate" />
</div>
}
<div id="chartimage">
@Html.Partial("ValidationSummary")
</div>
I then display a PartialView
that has a @Html.ValidationSummary()
in case of validation errors.
ReportController.cs
public PartialViewResult TotalPaymentsByMonthYear(MyViewModel model,
string reportName)
{
if (!ModelState.IsValid)
{
return PartialView("ValidationSummary", model);
}
model.ReportName = reportName;
return PartialView("Chart", model);
}
What I'd like to do is: instead of displaying validation errors within this PartialView
, I'm looking for a way of sending this validation error message to a DIV element that I have defined within the _Layout.cshtml
file.
_Layout.cshtml
<div id="message">
</div>
@RenderBody()
I'd like to fill the content of this DIV asynchronously. Is this possible? How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就我个人而言,我会扔掉
Ajax.*
帮助程序并这样做:然后我会使用自定义 HTTP 响应标头来指示发生了错误:
最后在一个单独的 javascript 文件中,我会不引人注目地 AJAXify 这个表单并在基于此自定义 HTTP 标头的存在的成功回调中,我会将结果注入其中一部分或另一部分:
Personally I would throw
Ajax.*
helpers away and do it like this:Then I would use a custom HTTP response header to indicate that an error occurred:
and finally in a separate javascript file I would unobtrusively AJAXify this form and in the success callback based on the presence of this custom HTTP header I would inject the result in one part or another: