如何访问Html.ValidationMessage()的文本以在Razor中检查它?

时间:2017-11-03 09:49:16

标签: c# razor validationmessage

我有一些代码可以预订电影票。如果有人已预订机票,他可以预订更多机票,仅在有特定错误消息时

此var validationSummary仅接收标记(<span class="field-validation-valid" data-valmsg-for="Saving" data-valmsg-replace="true"></span>)。但是在网页中@validationSummary打印正确的消息。 我如何才能访问错误消息的文本以进行检查?

@{
    @Html.ValidationSummary(true)
    var validationSummary = Html.ValidationMessage("Saving");
    bool buyAnotherVal = validationSummary.ToString().Contains("By continuing this form, you will booking another some tickets");
 }
 @using ( Html.BeginUmbracoForm<MovieBookingSurfaceController>( "SubmitBuyerDetails", new { buyAnother = buyAnotherVal }) )
 {
    @validationSummary

1 个答案:

答案 0 :(得分:0)

我解决了这个问题:

@Html.ValidationSummary(true)
var validationSummary = Html.ValidationMessage("Saving");
bool bookAnotherVal = Html.ValidationSummary().ToString().Contains(BookingException.Msg_EmailDuplicatedBookAnother);

ValidationSummary方法显示页面上所有验证消息的列表。 (https://msdn.microsoft.com/en-us//library/system.web.mvc.html.validationextensions.validationsummary(v=vs.118).aspx