2012-01-12 56 views
1

我有兩個模型類有一對多的關係。@ 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>'. 

每個建議將不勝感激。

回答

4

那麼這是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

2

爲什麼我回答我自己的問題是我不希望任何人像我已經找到的那樣面對同樣的問題。

首先,讓我對@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