Asp.net mvc MVC ValidationMessageFor Dons';在对象数组上不显示错误

Asp.net mvc MVC ValidationMessageFor Dons';在对象数组上不显示错误,asp.net-mvc,vb.net,Asp.net Mvc,Vb.net,考虑以下@razor页面: For i as Integer = 0 to 4 @Html.TextBoxFor(Function(model) model.Skills(i).Name) @Html.TextBoxFor(Function(model) model.Skills(i).Title) @Html.ValidationMessageFor(Function(model) model.Skills(i).Name) Next @Html.

考虑以下@razor页面:

 For i as Integer = 0 to 4
      @Html.TextBoxFor(Function(model) model.Skills(i).Name)  
      @Html.TextBoxFor(Function(model) model.Skills(i).Title)
      @Html.ValidationMessageFor(Function(model) model.Skills(i).Name)
 Next
 @Html.ValidationMessageFor(Function(model) model.Skills)  // <- This one doesnt display validation error message !!
i作为整数=0到4的

@Html.TextBoxFor(函数(模型)model.Skills(i).Name)
@Html.TextBoxFor(函数(模型)model.Skills(i).Title)
@Html.ValidationMessageFor(函数(模型)model.Skills(i).Name)
下一个

@Html.ValidationMessageFor(Function(model)model.Skills)/虽然我不确定以下示例代码是否回答了您的确切问题,但它非常适合使用DataAnnotations属性进行数组项长度验证:

型号

Imports System.ComponentModel.DataAnnotations

Public Class MyCustomObjectWithArray
    <MinLength(4, ErrorMessage:="Minimum four Employees should be submitted.")>
    Public Property EmployeeArray() As Employee()
End Class

Public Class Employee

    Public Property Name() As String

    Public Property City() As String

End Class
导入System.ComponentModel.DataAnnotations
公共类MyCustomObjectWithArray
公共属性EmployeeArray()作为雇员()
末级
公营雇员
作为字符串的公共属性名()
公共属性City()作为字符串
末级
控制器

Public Class HomeController
    Inherits System.Web.Mvc.Controller

    Function Index() As ActionResult

        Dim customObject As MyCustomObjectWithArray = New MyCustomObjectWithArray()
        customObject.EmployeeArray = New Employee() {}

        'Fill the employee array to have only 3 elements so that
        '       validation fail on form post since Employee array has validation for at least 4 employee names.
        For empIndex = 0 To 2
            ReDim Preserve customObject.EmployeeArray(empIndex)
            customObject.EmployeeArray(empIndex) = New Employee()
        Next

        Return View("CreateEmployees", customObject)
    End Function

    <HttpPost>
    Function CreateEmployees(ByVal objWithArray As MyCustomObjectWithArray) As ActionResult

        If (Not ModelState.IsValid) Then    'Make sure modelstate validation errors are not bypassed.
            Return View("CreateEmployees", objWithArray)
        End If

        'Do stuff like saving to DB or whatever you want with the valid Posted data.

        Return RedirectToAction("Index", "Home")
    End Function
End Class
公共类HomeController
继承System.Web.Mvc.Controller
函数Index()作为ActionResult
将customObject设置为MyCustomObjectWithArray=新建MyCustomObjectWithArray()
customObject.EmployeeArray=新员工(){}
'将employee数组填充为只有3个元素,以便
'表单发布验证失败,因为员工数组至少验证了4个员工姓名。
对于empIndex=0到2
ReDim保留customObject.EmployeeArray(empIndex)
customObject.EmployeeArray(empIndex)=新员工()
下一个
返回视图(“CreateEmployees”,customObject)
端函数
函数CreateEmployees(ByVal objWithArray作为MyCustomObjectWithArray)作为ActionResult
如果(不是ModelState.IsValid),则“确保没有绕过ModelState验证错误”。
返回视图(“CreateEmployees”,objWithArray)
如果结束
'使用有效的发布数据执行诸如保存到DB或任何您想要的操作。
返回重定向操作(“索引”、“主目录”)
端函数
末级
查看

@ModelType MvcVBApplication.MyCustomObjectWithArray

@Code
    ViewData("Title") = "CreateEmployees"
End Code

<h3>CreateEmployees<h3>

    @Using (Html.BeginForm("CreateEmployees", "Home"))

        Html.EnableClientValidation(True)
        @<table>
            @If (Model.EmployeeArray IsNot Nothing) Then

            For empIndex = 0 To Model.EmployeeArray.Count() - 1
                @<tr>
                    <td>
                        @Html.TextBoxFor(Function(modelItem) Model.EmployeeArray(empIndex).Name)
                    </td>
                </tr>
            Next

        Else
                @Html.TextBox("(0).Name", "")
        End If
        </table>
        @Html.ValidationMessageFor(Function(m) m.EmployeeArray)
        @<br />
        @<input type="submit" name="name" value="Submit" />
    End Using
@ModelType MvcVBApplication.MyCustomObjectWithArray
@代码
ViewData(“标题”)=“CreateEmployees”
结束代码
创造员工
@使用(Html.BeginForm(“CreateEmployees”、“Home”))
Html.EnableClientValidation(True)
@
@如果(Model.EmployeeArray不是空的),那么
对于Model.EmployeeArray.Count()-1,empIndex=0
@
@Html.TextBoxFor(函数(modelItem)Model.EmployeeArray(empIndex.Name)
下一个
其他的
@Html.TextBox(“(0.Name)”,“”)
如果结束
@Html.ValidationMessageFor(函数(m)m.EmployeeArray)
@
@ 终端使用

希望这有帮助。我不太喜欢VB.NET,所以如果您发现我使用的代码结构似乎是旧的,您可以选择适当的代码结构

如何为集合创建自定义验证属性?并且它不绑定到
技能[x]。名称
。为此,您需要
@Html.ValidationMessageFor(Function(model)model.Skills(i).Name)
这是正确的,有效的。我希望得到数组本身的错误消息,而不仅仅是数组中的元素。在我上面的示例中,我试图验证技能数组的最小长度,并且错误消息(如果它小于最小值)不会显示。我希望此链接能够帮助您(连接自定义验证并将其发送到客户端的方法)当前位置我不太明白你是怎么做的。您是如何为此创建验证属性的?它支持客户端验证吗?您是否为
model.Skills
提供了某种输入,以便验证可以与属性匹配?
@ModelType MvcVBApplication.MyCustomObjectWithArray

@Code
    ViewData("Title") = "CreateEmployees"
End Code

<h3>CreateEmployees<h3>

    @Using (Html.BeginForm("CreateEmployees", "Home"))

        Html.EnableClientValidation(True)
        @<table>
            @If (Model.EmployeeArray IsNot Nothing) Then

            For empIndex = 0 To Model.EmployeeArray.Count() - 1
                @<tr>
                    <td>
                        @Html.TextBoxFor(Function(modelItem) Model.EmployeeArray(empIndex).Name)
                    </td>
                </tr>
            Next

        Else
                @Html.TextBox("(0).Name", "")
        End If
        </table>
        @Html.ValidationMessageFor(Function(m) m.EmployeeArray)
        @<br />
        @<input type="submit" name="name" value="Submit" />
    End Using