@ Html.ValidationMessageFor没有按预期工作

问题描述:

我有两个模型类有一对多的关系。@ Html.ValidationMessageFor没有按预期工作

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; } 
} 

Razor文件。

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

1)我的问题的拳头,Validaion功能没有火的时候,我选择选择循环型,而且只给回错误的

The ViewData item that has the key 'CycleTypeID' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'. 

2)第二个问题是,当我选择一个的循环类型,并把值设置为超过20个字符,以便验证器可以按照我的预期进行检查。但是我又收到了同样的错误信息。

The ViewData item that has the key 'CycleTypeID' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'. 

每个建议将不胜感激。

那么这是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 

,并考虑用这个

@Html.DropDownList("CycleTypeId", String.Empty) 

更多更新

有Ø解决此问题的更多解决方法。

只需使用您的原始代码DropDownListFor。然后就是利用类属性的它像下面

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

这将使验证工作,但它会显示该字段需要的默认消息。但是否则会按照您的预期工作。

我想这是一个更好的解决方案,适合您的需求。

+0

感谢您的有益解决方案@Pankaj Upadhyay。我认为HTML.DropDownList无法加载像“(SelectList)ViewBag.CycleType”的数据集合。所以你可以告诉我最好的解决方案。 – 2012-01-12 06:43:52

+0

我已经用DropDownList的用法更新了答案。让我知道如果它不起作用。目前无法在我的系统上进行测试 – 2012-01-12 06:55:24

+0

非常感谢你@Pankaj Upadhyay,这是工作,但其中一个问题是无法根据现有值动态选择其中一项。我可以通过使用“@ Html.DropDownListFor(model => model.CycleTypeID, ...”。谢谢 – 2012-01-12 07:10:34

为什么我回答我自己的问题是我不希望任何人像我已经找到的那样面对同样的问题。

首先,让我对@Pankaj Upadhyay说,我真的很感谢他的帮助。我非常感谢你@Pankaj Upadhyay。

最后,我可以通过从@Pankaj Upadhyay获得持续帮助来解决我的问题。

@model CyclingClubSystem.Models.CycleModel 

@{ 
ViewBag.Title = "Edit"; 
Layout = "~/Views/Shared/_Layout.cshtml"; 
HtmlHelper.ClientValidationEnabled = true; 
HtmlHelper.UnobtrusiveJavaScriptEnabled = true; 

} 

<h2>Edit</h2> 

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 

@using (Html.BeginForm()) { 
@Html.ValidationSummary(true) 
<fieldset> 
    <legend>CycleModel</legend> 

    @Html.HiddenFor(model => model.CycleModelID) 

    <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", @class = "required" })*@ 

     @Html.DropDownListFor(model => model.CycleTypeID, 
         (SelectList)ViewBag.CycleType, 
         "Select Cycle Type", 
         new { id = "ddlCycleType"}) 

     @*Html.DropDownList("CycleType", "Select Cycle Type")*@ 
     @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> 

    <p> 
     <input type="submit" value="Save" /> 
    </p> 
    </fieldset> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

控制器类

[HttpGet] 
    public ActionResult Edit(int CycleModelID) 
    { 
     CycleModel cycleModel = unitOfWork_CycleModel.GenericTEntityRepository.GetByID(CycleModelID); 
     //ViewBag.CycleType = new SelectList(unitOfWork_cycleType.GenericTEntityRepository.Get(orderBy: CycleTypes => CycleTypes.OrderBy(CycleType => CycleType.Type)), "CycleTypeID", "Type", cycleModel.CycleTypeID); 
     ViewBag.CycleType = new SelectList(unitOfWork_cycleType.GenericTEntityRepository.Get(orderBy: CycleTypes => CycleTypes.OrderBy(CycleType => CycleType.Type)), "CycleTypeID", "Type"); 
     return View(cycleModel); 
    } 

    [HttpPost] 
    public ActionResult Edit(CycleModel _CycleModel) 
    { 
     if (ModelState.IsValid) 
     { 
      unitOfWork_CycleModel.GenericTEntityRepository.Update(_CycleModel); 
      unitOfWork_CycleModel.Save(); 
      return RedirectToAction("Index"); 
     } 
     ViewBag.CycleType = new SelectList(unitOfWork_cycleType.GenericTEntityRepository.Get(orderBy: CycleTypes => CycleTypes.OrderBy(CycleType => CycleType.Type)), "CycleTypeID", "Type"); 
     return View(_CycleModel); 
    } 

最后,我发现了什么,最主要的原因造成的错误,是我忘了把这些代码在[控制器编辑Post方法]

ViewBag.CycleType = new SelectList(unitOfWork_cycleType.GenericTEntityRepository.Get(orderBy: CycleTypes => CycleTypes.OrderBy(CycleType => CycleType.Type)), "CycleTypeID", "Type"); 

为什么我可以解决我的问题是我从@Pankaj Upadhyay获得持续的指导。

+0

我很高兴我可以帮忙!! ...谢谢,祝你好运 – 2012-01-12 09:57:57

+1

+1让我们知道你的目标是什么解决方案,这也帮助了我,所以也谢谢你,@Frank Myat T虎! – RvdV79 2013-07-17 08:26:56