Reputation: 68840
I am trying to build a helper which can execute this logic:
if (ViewData.ModelState[""] != null && ViewData.ModelState[""].Errors.Any())
{
<div class="note note-danger">
<h4 class="block">Errors</h4>
<p>@Html.ValidationSummary()</p>
</div>
}
It would need access to the ViewData and the Html.ValidationSummary
Do these need to be sent into the Helper, of can the Helper access them in some way by some base class?
My Helper:
public static class ValidationSummaryHelper
{
public static HtmlString Summary(???)
{ }}
Upvotes: 3
Views: 1892
Reputation: 2126
Same in C#
public static class ValidationExtensions
{
public static MvcHtmlString AddCustomValidationSummary(this HtmlHelper htmlHelper)
{
string result = "";
if (htmlHelper.ViewData.ModelState[""] != null && htmlHelper.ViewData.ModelState[""].Errors.Any())
{
result = "<div class='note note-danger'><h4 class='block'>Errors</h4><p>" + htmlHelper.ValidationSummary().ToString() + "</p></div>";
}
return new MvcHtmlString(result);
}
}
Upvotes: 2
Reputation: 33738
I don't know the syntax for it in C#, but in VB it would look like this:
<Extension>
Public Function AddCustomValidationSummary(htmlHelper As HtmlHelper) As MvcHtmlString
Dim result As String = String.Empty
If (htmlHelper.ViewData.ModelState("") Is Nothing) AndAlso (htmlHelper.ViewData.ModelState("").Errors.Any()) Then
result = "<div class='note note-danger'><h4 class='block'>Errors</h4><p>" & htmlHelper.ValidationSummary().ToString() & "</p></div>"
End If
Return New MvcHtmlString(result)
End Function
Use it like:
@Html.AddCustomValidationSummary()
Upvotes: 3