问题描述
我的工作与Asp.Net MVC3
项目在一个视图我有 @ Html.ValidationSummary(真)
和它通常会产生
< DIV CLASS =验证,汇总错误>
< UL>
<立GT;中发生了一点<!/李>
< / UL>
< / DIV>
如何延长这个的ValidationSummary到MyValidationSummary和产生的HTML code ++模板是这样的:
< DIV CLASS =通知警告>
<跨度>< / SPAN>
< DIV CLASS =文本> < P>中发生了一点<!/ P> < / DIV>
< / DIV>
这question详细编写自定义验证总结的过程。
修改
这将做你想要什么:
公共静态类LinqExt
{
静态字符串MyValidationSummary(此的HtmlHelper帮手,串validationMessage =)
{
字符串retVal的=;
如果(helper.ViewData.ModelState.IsValid)
返回;
retVal的+ =< DIV CLASS ='通知的警告'><跨度>中;
如果(!String.IsNullOrEmpty(validationMessage))
retVal的+ = helper.En code(validationMessage);
retVal的+ =< / SPAN>中;
retVal的+ =< DIV CLASS ='文本'>中;
的foreach(在helper.ViewData.ModelState.Keys VAR键)
{
的foreach(在helper.ViewData.ModelState VAR错误[关键] .Errors)
retVal的+ =&所述; P>中+ helper.En code(err.ErrorMessage)+< / P>中;
}
retVal的+ =< / DIV>< / DIV>中;
返回retVal.ToString();
}
}
在code是自我解释;刚刚经历的ModelState错误列举,并在您选择的DOM元素包装的错误。还有就是如果我使用它像一个错误:
<%:Html.MyValidationSummary()%GT;
这将在页面文本上显示HTML标签,而不是渲染它。
<%= Html.MyValidationSummary()%GT;
这工作得很好。
I am working on a project with Asp.Net MVC3
In a View I have @Html.ValidationSummary(true)
and as usually it produces
<div class="validation-summary-errors">
<ul>
<li>Something bad Happened!</li>
</ul>
</div>
How can I extend this ValidationSummary to MyValidationSummary and produces the Html Code template something like this:
<div class="notification warning">
<span></span>
<div class="text"> <p>Something bad Happened!</p> </div>
</div>
This question details the procedure of writing custom validation summary.
EDITThis will do what you want:
public static class LinqExt
{
static string MyValidationSummary(this HtmlHelper helper, string validationMessage="")
{
string retVal = "";
if (helper.ViewData.ModelState.IsValid)
return "";
retVal += "<div class='notification-warnings'><span>";
if (!String.IsNullOrEmpty(validationMessage))
retVal += helper.Encode(validationMessage);
retVal += "</span>";
retVal += "<div class='text'>";
foreach (var key in helper.ViewData.ModelState.Keys)
{
foreach(var err in helper.ViewData.ModelState[key].Errors)
retVal += "<p>" + helper.Encode(err.ErrorMessage) + "</p>";
}
retVal += "</div></div>";
return retVal.ToString();
}
}
The code is self explanatory; just enumerating through modelstate errors and wrapping errors in dom element of your choice. There is an error that is if i use it like:
<%:Html.MyValidationSummary()%>
It will display html tags on the page as text rather than rendering it.
<%=Html.MyValidationSummary()%>
This works fine.
这篇关于自定义的ValidationSummary模板Asp.net MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!