MVC ValidationMessageFor

时间:2011-01-27 13:20:32

标签: asp.net-mvc

我想在验证失败时显示图像而不是文本,但是ValidationMessageFor会对我的'validationMessage'进行编码。 我正在尝试使用普通的img标签声明来指定validationMessage 我怎么能这样做?

由于

2 个答案:

答案 0 :(得分:1)

您需要创建自己的HTMLHelper。

类似的东西:

 public static MvcHtmlString   ValidationImage(this HtmlHelper htmlHelper)
    {

        if (!htmlHelper.ViewData.ModelState.IsValid)
        {
                  //todo: strip out the information you need from the model and  return <img/> tag(s).    
        }

        return null;

    }

答案 1 :(得分:0)

你可以声明一个包含html格式的图像的const字符串,如:

const string ERROR_IMAGE = "<img src=\"...\"  alt=\"\" />";

在“查看操作”中,您可以执行以下操作:

if(Something is Ine error)
   ModelState.AddModelError(Something, ERROR_IMAGE);
相关问题