2011-01-27 80 views
0

我想在驗證失敗時顯示圖像而不是文本,但ValidationMessageFor對我的'validationMessage'進行編碼。 我想使用普通的img標籤聲明指定validationMessage 我該怎麼做?MVC ValidationMessageFor

謝謝

+0

你的意思是像''? – 2011-01-27 13:21:48

+0

編輯...對不起 – TiagoDias 2011-01-27 13:27:04

回答

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; 

    } 
0

你可以聲明包含圖像的常量字符串在HTML格式,如:在查看操作

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

你可以做這樣的事情:

if(Something is Ine error) 
    ModelState.AddModelError(Something, ERROR_IMAGE); 
相關問題