我開發一個Asp.Net MVC 4應用程序,我有這兩個示範類:Asp.Net MVC 4 ModelState.IsValid返回False
CompanyModel:
Public Class CompanyModel
<Key()>
Public Overridable Property CompanyID As Integer
<Required(ErrorMessage:="Enter a company name.")>
<Display(Name:="Company")>
<StringLength(80, ErrorMessage:="The {0} must have {2} characters.", MinimumLength:=2)>
Public Overridable Property Name As String
<Required(AllowEmptyStrings:=False, ErrorMessage:="Enter the DB ID.")>
<Display(Name:="DB ID")>
Public Overridable Property DBID As Integer
End Class
PersonModel:
Public Class PersonModel
<Key()>
Public Overridable Property PersonID As Integer
<Required(AllowEmptyStrings:=False, ErrorMessage:="Enter the name.")>
<Display(Name:="Name")>
Public Overridable Property Name As String
<Required(AllowEmptyStrings:=False, ErrorMessage:="Enter the pass")>
<DataType(DataType.Password)>
<Display(Name:="Password")>
Public Overridable Property Password As String
<Required(AllowEmptyStrings:=False, ErrorMessage:="Enter the mail")>
<Display(Name:="E-mail")>
Public Overridable Property Email As String
<Required(ErrorMessage:="Enter the company")>
<Display(Name:="Company")>
Public Overridable Property BcoID As CompanyModel
End Class
PersonController:
'
' POST: /Person/Create
<HttpPost()> _
Function Create(ByVal model As PersonModel) As ActionResult
Try
If ModelState.IsValid Then
personDAO.Save(model)
End If
Return RedirectToAction("Index")
Catch
Return View()
End Try
End Function
我在查看/人驗證碼/創建:
<div class="editor-label">
@Html.LabelFor(Function(model) model.BcoID)
</div>
<div class="editor-field">
@Html.DropDownListFor(Function(model) model.BcoID.CompanyID, New SelectList(ViewBag.Company, "CompanyID", "Name"))
@Html.ValidationMessageFor(Function(model) model.BcoID)
</div>
當我創造新的人,在PersonController當我嘗試保存我得到ModelState.Isvalid =假消息是「輸入一個公司名稱「。我不知道爲什麼會發生。
有人可以幫助我嗎?
編輯
如果我改變我從model.BcoID.CompanyID到model.BcoID.Name財產,我使用EditorFor,而不是使用DropDownListFor。 ModelState.IsValid是True,但我需要一個DropDownList,所以我放了一個DropDownList,現在我有一個驗證按摩(「公司必須有2個字符。」)在我的視圖。
這是我現在有:
<div class="editor-label">
@Html.LabelFor(Function(model) model.BcoID.Name)
</div>
<div class="editor-field">
@Html.DropDownListFor(Function(model) model.BcoID.Name, New SelectList(ViewBag.Company, "CompanyID", "Name"))
@Html.ValidationMessageFor(Function(model) model.BcoID.Name)
</div>
我編輯顯示我的更改。 –
你似乎正在使用一些我在你的模型中看不到的'Nome'屬性。在你的模型中有一個名稱屬性。 –
對不起,現在我翻譯的屬性。 –