我正在测试一个事情的列表为null.每次找到一个,我将它保存在一个数组中,以便在一个验证消息中实现它.
输出我想看起来像这样:
字段1是必需的
需要领域4
等等…
但我似乎无法开始一条新线.
现在,看起来像这样:
字段1是必需的字段4是必需的
有人知道如何实现这一点吗?
编辑:
控制器:
IDictionary<int,String> emptyFields = new Dictionary<int,String>(); foreach (Something thing in AnotherThing.Collection) { if (thing.Property == null) emptyFields.add(thing.Index,thing.Name); } if (emptyFields.Any()) throw new CustomException() { EmptyFields = emptyFields };
此异常在此处理:
catch (CustomException ex) { ModelState.AddModelError("file",ex.GetExceptionString()); return View("theView"); }
CustomException:
public class CustomException: Exception { public IDictionary<int,String> EmptyFields { get; set; } public override String Label { get { return "someLabel"; } } public override String GetExceptionString() { String msg = ""; foreach (KeyValuePair<int,String> elem in EmptyFields) { msg += "row: " + (elem.Key + 1).ToString() + " column: " + elem.Value + "<br/>"; } return msg; } }
视图:
<span style="color: #FF0000">@Html.Raw(Html.ValidationMessage("file").ToString())</span>
解决方法
您将需要编写一个自定义帮助器来实现.内置的ValidationMessageFor帮助器自动将HTML编码为值.这里有一个例子:
public static class ValidationMessageExtensions { public static IHtmlString MyValidationMessageFor<TModel,TProperty>( this HtmlHelper<TModel> htmlHelper,Expression<Func<TModel,TProperty>> ex ) { var htmlAttributes = new RouteValueDictionary(); string validationMessage = null; var expression = ExpressionHelper.GetExpressionText(ex); var modelName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(expression); var formContext = htmlHelper.ViewContext.ClientValidationEnabled ? htmlHelper.ViewContext.FormContext : null; if (!htmlHelper.ViewData.ModelState.ContainsKey(modelName) && formContext == null) { return null; } var modelState = htmlHelper.ViewData.ModelState[modelName]; var modelErrors = (modelState == null) ? null : modelState.Errors; var modelError = (((modelErrors == null) || (modelErrors.Count == 0)) ? null : modelErrors.FirstOrDefault(m => !String.IsNullOrEmpty(m.ErrorMessage)) ?? modelErrors[0]); if (modelError == null && formContext == null) { return null; } var builder = new TagBuilder("span"); builder.MergeAttributes(htmlAttributes); builder.AddCssClass((modelError != null) ? HtmlHelper.ValidationMessageCssClassName : HtmlHelper.ValidationMessageValidCssClassName); if (!String.IsNullOrEmpty(validationMessage)) { builder.InnerHtml = validationMessage; } else if (modelError != null) { builder.InnerHtml = GetUserErrorMessageOrDefault(htmlHelper.ViewContext.HttpContext,modelError,modelState); } if (formContext != null) { bool replaceValidationMessageContents = String.IsNullOrEmpty(validationMessage); builder.MergeAttribute("data-valmsg-for",modelName); builder.MergeAttribute("data-valmsg-replace",replaceValidationMessageContents.ToString().ToLowerInvariant()); } return new HtmlString(builder.ToString(TagRenderMode.Normal)); } private static string GetUserErrorMessageOrDefault(HttpContextBase httpContext,ModelError error,ModelState modelState) { if (!String.IsNullOrEmpty(error.ErrorMessage)) { return error.ErrorMessage; } if (modelState == null) { return null; } var attemptedValue = (modelState.Value != null) ? modelState.Value.AttemptedValue : null; return string.Format(CultureInfo.CurrentCulture,"Value '{0}' not valid for property",attemptedValue); } }
接着:
public class Myviewmodel { [required(ErrorMessage = "Error Line1<br/>Error Line2")] public string SomeProperty { get; set; } }
并认为:
@model Myviewmodel @using (Html.BeginForm()) { @Html.EditorFor(x => x.SomeProperty) @Html.MyValidationMessageFor(x => x.SomeProperty) <button type="submit">OK</button> }
如果要在ValidationSummary中显示错误消息,您还可以编写一个自定义帮助器,不会像this post
中显示的那样对错误消息进行HTML编码.