c# - 没有 html 标记的 ASP.NET MVC 3/4 ValidationMessageFor

标签 c# asp.net-mvc asp.net-mvc-4 razor

有没有办法利用 Html.ValidationMessageFor() 返回验证文本而不带 HTML 标记?

当前代码:

@Html.ValidationMessageFor(m => m.SomeProperty)

返回:

<span class="field-validation-error" data-valmsg-for="Model.SomeProperty" data-valmsg-replace="true">This field is required.</span>

我更喜欢这个:

This field is required.

最佳答案

m0s 的评论让我看到了这个 StackOverflow 问题/答案。 (How do I get the collection of Model State Errors in ASP.NET MVC?)

这可能是多余的,但这是我的工作解决方案:

@if (ViewData.ModelState.ContainsKey("SomeProperty))
{
     @Html.TextBoxFor(m => m.SomeProperty), 
          new {
               @some_attribute = ViewData.ModelState["SomeProperty"].Errors[0].ErrorMessage })     
}

显然,您希望在解决第一个错误之前确保您的 ModelState 有错误,在我的情况下,这始终是正确的。

关于c# - 没有 html 标记的 ASP.NET MVC 3/4 ValidationMessageFor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16425735/

相关文章:

javascript - 从 Controller 中的 View 读取 HTML 值

c# - MVC 4 如何显示注解

c# - 如何在同一项目中使用 MVC Controller 和 WebAPI Controller

c# - .NET Core 2.2 中的 HttpClient 等效吗?

c# - 如何以编程方式检查 NServiceBus 是否已完成所有消息的处理

c# - Sitecore 以编程方式访问布局定义

css - 创建动态 div Razor MVC

C# 在屏幕顶部中心定位窗口

asp.net-mvc - 是否可以将 ASP.NET 成员(member)资格提供程序部署到 Azure SQL 数据库以与 Azure 网站 MVC 应用程序一起使用?

c# - CS1003 : Syntax error, '>' 预计在 Razor 中