ModelState.AddModelError EN codeS HTML [英] ModelState.AddModelError encodes HTML

查看:131
本文介绍了ModelState.AddModelError EN codeS HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ModelState.AddModelError来验证我的形式输入时注意到一个奇怪的问题。从Html.ValidationMessage输出是不是真正的HTML值,但它的EN codeD值等CSS样式不应用于该错误消息。

I am noticing a weird issue when using ModelState.AddModelError to validate input on my forms. The output from Html.ValidationMessage is not the true HTML value but it's encoded value and so the CSS style is not applied to the error message.

例如:

private string errorMessage = "<span class=\"negative\">{0}</span><br class=\"hid\" />";
ModelState.AddModelError("title", String.Format(errorMessage, "Tab title is required"));

的输出被示为:

<span class="field-validation-error">&lt;span class=&quot;negative&quot;&gt;URL is Required&lt;/span&gt;&lt;br class=&quot;hid&quot; /&gt;</span>

这没有用是他们的早期测试的情况下,我不知道在这里采取什么办法。

This didn't use to be the case with their earlier beta's and I am not sure what approach to take here.

谢谢
尼克

推荐答案

有另一种方式来做到这一点,也无需创建您自己的扩展。

There is another way to do it, too, without having to create your own extension.

例如说我们在我们的控制器之一以下内容:

Say for instance we have the following in one of our controllers:

ModelState.AddModelError("Name", "<b>Please Use a Valid Person Name</b>");

我们可以再做,我们认为以下几点:

We can then do the following in our view:

@if(Html.ValidationMessageFor(x => x.Name) != null){
    @Html.Raw(Html.ValidationMessageFor(x => x.Name).ToString())
}

意志prevent 的错误信息'&LT; B&gt;请使用有效的人名&LT; / B&GT; 被连接codeD。

这篇关于ModelState.AddModelError EN codeS HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆