我正在尝试构建一个可以执行此逻辑的助手:
if (ViewData.ModelState[""] != null && ViewData.ModelState[""].Errors.Any())
{
<div class="note note-danger">
<h4 class="block">Errors</h4>
<p>@Html.ValidationSummary()</p>
</div>
}
它需要访问 ViewData 和 Html.ValidationSummary
是否需要将这些发送到 Helper,Helper 是否可以通过某些基类以某种方式访问它们?
我的 helper :
public static class ValidationSummaryHelper
{
public static HtmlString Summary(???)
{ }}
最佳答案
我不知道它在 C# 中的语法,但在 VB 中它看起来像这样:
<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
像这样使用它:
@Html.AddCustomValidationSummary()
关于c# - MVC 4 - 构建访问 ViewData 的 Helper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20671116/