@ Html.ValidationMessageFor未能按预期工作

Fra*_*Thu 1 c# validation asp.net-mvc entity-framework

我有两个模型类,它们之间具有一对多的关系。

public class CycleType
{
    [Required(ErrorMessage = "Cycle is required.")]
    public int CycleTypeID { get; set; }

    [Required(ErrorMessage = "Cycle Type is required.")]
    [StringLength(20, ErrorMessage = "Cycle Type may not be longer than 20 characters")]
    public string Type { get; set; }

    public List<CycleModel> CycleModels { get; set; }
}

public class CycleModel
{
    public int CycleModelID { get; set; }

    [DisplayName("Cycle Type")]
    [Required(ErrorMessage = "Cycle is required.")]
    public int CycleTypeID { get; set; }

    [Required(ErrorMessage = "Model is required.")]
    [StringLength(20, ErrorMessage = "Model may not be longer than 20 characters")]
    public string Model { get; set; }

    public virtual CycleType CycleType { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

剃刀文件。

    <div class="editor-label">
        @Html.LabelFor(model => model.CycleTypeID)
    </div>
    <div class="editor-field">            
        @Html.DropDownListFor(model => model.CycleTypeID,
                                (SelectList)ViewBag.CycleType,
                                "Select Cycle Type",
                              new { id = "ddlCycleType" })
        @Html.ValidationMessageFor(model => model.CycleTypeID)
    </div>


    <div class="editor-label">
        @Html.LabelFor(model => model.Model)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Model)
        @Html.ValidationMessageFor(model => model.Model)
    </div>
Run Code Online (Sandbox Code Playgroud)

1)我的问题的拳头是,当我选择选择循环类型时,Validaion函数没有触发,并且仅将错误返回给

The ViewData item that has the key 'CycleTypeID' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.
Run Code Online (Sandbox Code Playgroud)

2)第二个问题是,当我选择一种循环类型并将值建模为超过20个字符时,验证器可以按我的预期进行检查。但我再次收到相同的错误消息。

The ViewData item that has the key 'CycleTypeID' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.
Run Code Online (Sandbox Code Playgroud)

每个建议将不胜感激。

Pan*_*yay 5

好吧,这是Asp.net MVC中的错误,由于该错误,DropDownListFor和TextAreaFor扩展方法的验证无效。

您可以在http://aspnet.codeplex.com/workitem/8576中查看详细信息

因此,您将需要使用HTML.DropDownList而不是DropDownListFor,然后您的验证将按预期进行。

更新资料

从您的控制器,传递此

ViewBag.CycleTypeId = new SelectList(db.CycleTypes, "CycleTypeId", "Type"); // db is ur context instance
Run Code Online (Sandbox Code Playgroud)

并在视图中使用此

@Html.DropDownList("CycleTypeId", String.Empty)
Run Code Online (Sandbox Code Playgroud)

更多更新

此问题还有另一种解决方法。

只需使用的原始代码即可DropDownListFor。然后像下面这样使用class属性

 @Html.DropDownListFor(model => model.CycleTypeID,
                            (SelectList)ViewBag.CycleType,
                            "Select Cycle Type",
                          new { id = "ddlCycleType", @class = "required" })
Run Code Online (Sandbox Code Playgroud)

这将使验证工作正常进行,但将显示默认消息“必填字段”。但其他方法将按您预期的那样工作。

我想这是一个更好的解决方案,并且可以满足您的需求。