【问题标题】:Asp.Net MVC 4 ModelState.IsValid return FalseAsp.Net MVC 4 ModelState.IsValid 返回 False
【发布时间】:2013-01-31 16:17:02
【问题描述】:

我正在开发一个 Asp.Net MVC 4 应用程序,我有这两个模型类:

公司型号:

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

人物模型:

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

人员控制器:

'
' 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

我在 Views/Person/Create 中有此代码:

<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 = False,消息是“输入公司名称”。我不知道为什么会这样。

有人可以帮帮我吗?

编辑

如果我将我的属性从 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>

【问题讨论】:

    标签: vb.net razor asp.net-mvc-4


    【解决方案1】:

    确保您的视图中有一个公司名称属性,因为您的模型中需要该属性:

    <div class="editor-label">
        @Html.LabelFor(Function(model) model.BcoID.Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(Function(model) model.BcoID.Name)
        @Html.ValidationMessageFor(Function(model) model.BcoID.Name)
    </div>
    

    此外,Label 和 ValidationMessage 助手附加到您的 CompanyID 属性的错误属性。应该是这样的:

    <div class="editor-label">
        @Html.LabelFor(Function(model) model.BcoID.CompanyID)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(Function(model) model.BcoID.CompanyID, New SelectList(ViewBag.Company, "CompanyID", "Name"))
        @Html.ValidationMessageFor(Function(model) model.BcoID.CompanyID)
    </div>
    

    注意 Label 和 ValidationMessage 现在如何正确关联到 DropDownList。

    【讨论】:

    • 您似乎使用了一些我在您的模型中看不到的 Nome 属性。在您的模型中有一个 Name 属性。
    • 对不起,我现在翻译属性。
    • 您是否在视图中添加了@Html.EditorFor(Function(model) model.BcoID.Name),如我的回答所示?
    • 但是您已经有一个绑定到 CompanyID 属性的 DropDownList。这是您提交表单时发送到服务器的唯一信息。这就是 HTML 中的 &lt;select&gt; 元素的工作原理。它永远不会发送名称。因此,您必须从 Name 属性中删除 Required 属性并使用 CompanyID 属性(应该是必需的)才能从数据存储中检索相应的公司名称。
    猜你喜欢
    • 2011-02-22
    • 2018-02-16
    • 2013-09-08
    • 2013-06-28
    • 2019-09-11
    • 2012-11-02
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多