gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:04:25 25 4
gpt4 key购买 nike

有没有一种方法可以利用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com