2014-02-13 90 views
1

我有三個模型:VehicleType,VehicleModelVehicleManufacturer多個模型的MVC客戶端驗證

兩個VehicleTypeVehicleManufacturerVehicleModel在模型中,像這樣:

public class VehicleModel 
{ 
    [Key] 
    public int ModelId { get; set; } 

    [Required(ErrorMessage = "Field is Required")] 
    public int TypeId { get; set; } 

    [Required(ErrorMessage = "Field is Required")] 
    public int ManufacturerId { get; set; } 
    public string ModelName { get; set; } 


    public VehicleType VehicleType { get; set; } 
    public VehicleManufacturer Manufacturer { get; set; } 

} 

從那裏,VehicleModel指向InventoryModel:

public class Inventory 
{ 
    [Key] 
    public int InventoryId { get; set; } 
    public int Price { get; set; } 
    public int Mileage { get; set; } 
    public int Year { get; set; } 


    public int ModelId { get; set; } 
    public VehicleModel VehicleModel { get; set; } 
} 

我的問題是,當我試圖讓客戶端所有三個dropdownlists(VehicleType,VehicleManufacturer,VehicleModel)工作驗證,它只適用於VehicleModel

需要做什麼來驗證使用這些模型的這兩個dropdownlist?

這裏是我的控制器(僅供參考):

// GET: /Inventory/Create 
    public ActionResult Create() 
    { 
     ViewBag.TypeId = new SelectList(db.Types, "TypeId", "TypeName"); //(Object List, Value Field (usually Id), Column) 
     ViewBag.ModelId = new SelectList(db.Models, "ModelId", "ModelName"); //(Object List, Value Field (usually Id), Column) 
     ViewBag.ManufacturerId = new SelectList(db.Manufacturers, "ManufacturerId", "ManufacturerName"); //(Object List, Value Field (usually Id), Column) 

     return View(); 
    } 

    // POST: /Inventory/Create 
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598. 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create(Inventory inventory, VehicleManufacturer VehicleManufacturer, VehicleType VehicleType) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Inventorys.Add(inventory); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     ViewBag.TypeId = new SelectList(db.Types, "TypeId", "TypeName"); 
     ViewBag.ModelId = new SelectList(db.Models, "ModelId", "ModelName"); 
     ViewBag.ManufacturerId = new SelectList(db.Manufacturers, "ManufacturerId",  "ManufacturerName"); 

     return View(inventory); 
    } 

查看:

<div class="editor-label"> 
     @Html.LabelFor(model => model.VehicleModel.TypeId, "Some name for column") 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownList("TypeId", String.Empty) 

     @Html.ValidationMessageFor(model => model.VehicleModel.TypeId) 
    </div> 


    <div class="editor-label"> 
     @Html.LabelFor(model => model.ModelId, "Some name for column") 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownList("ModelId", String.Empty) 
     @Html.ValidationMessageFor(model => model.ModelId) 
    </div> 



    <div class="editor-label"> 
     @Html.LabelFor(model => model.VehicleModel.ManufacturerId, "Some name for column") 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownList("ManufacturerId", String.Empty) 
     @Html.ValidationMessageFor(model => model.VehicleModel.ManufacturerId) 
    </div> 

請人幫忙。我已經在這很多很多小時了!

+0

我沒有看到任何驗證屬性,您是否忘記在此處添加它或將它們全部忘在一起 – hjavaher

+1

您實際上沒有任何驗證屬性。另外,要小心使用'RequiredAttribute'來驗證一個值類型,因爲它們總是會有一個默認的值(因此總會有一個值)。 –

+0

對不起,傢伙,有驗證,我只是沒有添加到問題 – Batsu

回答

1

實際上有兩個問題,我看到上面

1)你不是映射的DropDownList和ValidationMessageFor相同的模型屬性。

@Html.ValidationMessageFor(model => model.VehicleModel.ManufacturerId) 

上述被綁定到VehicleModel_ManufacturerId,其中爲:

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

上述被映射的下拉菜單,只是ManufacturerId

您需要更改一個或另一個相互匹配。

2)在上面的代碼中,我沒有看到任何驗證屬性。當你在這裏複製代碼時,你忘了它們嗎?

希望這可以幫助,讓我知道如果你需要更多的細節。

+0

當我以這種方式匹配它們時:@ Html.DropDownList(「VehicleModel.ManufacturerId」,String.Empty) @ Html.ValidationMessageFor(model => model。 VehicleModel.ManufacturerId) 錯誤:沒有類型爲'IEnumerable '的ViewData項具有'VehicleModel'鍵。ManufacturerId 當我嘗試其他方式: @ Html.DropDownList( 「ManufacturerId」 的String.Empty) @ Html.ValidationMessageFor(型號=> model.ManufacturerId) CS1061: 'Dealership.Models.Inventory' 不包含'ManufacturerId'的定義並且沒有擴展方法'ManufacturerId' – Batsu

+0

對不起,格式爲 – Batsu

+0

爲什麼不直接使用'@ Html.DropDownListFor(model => model.VehicleModel.ManufacturerId,ViewBag.ManufacturerId)' – hjavaher