Asp.net
如何在 ASP.NET MVC 中擴展 ValidationSummary HTML Helper?
我需要將驗證摘要包裝在div中。當出現錯誤時,如何設置驗證摘要以用div包裝它?
<div class="validation-summary"> <%= Html.ValidationSummary("Login was unsuccessful. Please correct the errors and try again.") %> </div>
我不得不在我的另一個項目中擴展驗證摘要擴展,以處理頁面上的多個表單。
儘管這有所不同,但您可以創建自己的擴展方法…
namespace System.Web.Mvc { public static class ViewExtensions { public static string MyValidationSummary(this HtmlHelper html, string validationMessage) { if (!html.ViewData.ModelState.IsValid) { return "<div class=\"validation-summary\">" + html.ValidationSummary(validationMessage) + "</div>" } return ""; } } }然後打電話
<%= Html.MyValidationSummary( "Login was unsuccessful. Please correct the errors and try again.") %>HTH,查爾斯