到目前为止它正在运行,但我似乎无法按照正确的顺序确定HTML。
对于每个不同的错误,我需要返回此HTML:
<div class="style-msg errormsg">
<div class="sb-msg">
<i class="icon-remove"></i>
Error Text
</div>
</div>
这是扩展方法:
public static string MyValidationSummary(this HtmlHelper helper, string validationMessage = "")
{
string retVal = "";
if (helper.ViewData.ModelState.IsValid)
return "";
retVal += "<div class='style-msg errormsg'><div class='sb-msg'><i class='icon-remove'></i>";
if (!String.IsNullOrEmpty(validationMessage))
retVal += helper.Encode(validationMessage);
foreach (var key in helper.ViewData.ModelState.Keys)
{
foreach (var err in helper.ViewData.ModelState[key].Errors)
retVal += helper.Encode(err.ErrorMessage);
}
retVal += "</div></div>";
return retVal.ToString();
}
}
最后,我将在实际网页上调用它。
@Html.Raw(Html.MyValidationSummary())
奖励问:
在这种情况下使用HTML.Raw
是否安全?
答案 0 :(得分:0)
在玩了一下之后,我得出了这个结论。我稍微更改了HTML,因为我想使用Bootstrap警告框。
public static string MyValidationSummary(this HtmlHelper helper, string validationMessage = "")
{
string retVal = "";
string errorList = "";
foreach (var key in helper.ViewData.ModelState.Keys)
{
retVal = "";
foreach (var err in helper.ViewData.ModelState[key].Errors)
{
retVal += "<div class='alert alert-danger'>";
retVal += "<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>";
retVal += "<i class='icon-remove-sign'></i>";
retVal += helper.Encode(err.ErrorMessage);
retVal += "</div>";
errorList += retVal;
}
}
return errorList.ToString();
}