[英]MVC client-side validation for multiple models
我有三個模型: VehicleType
, VehicleModel
和VehicleManufacturer
。
VehicleType
和VehicleManufacturer
指向模型中的VehicleModel
,如下所示:
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; }
}
我的問題是,當我嘗試在所有三個下拉列表( VehicleType
, VehicleManufacturer
, VehicleModel
)上進行客戶端驗證時,它僅與VehicleModel
一起VehicleModel
。
使用這些模型需要做些什么來驗證這兩個下拉列表?
這是我的控制器(fyi):
// 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>
請有人幫忙。 我已經花了很多小時了!
我上面確實看到了兩個問題
1)您沒有將DropDownList和ValidationMessageFor映射到同一模型屬性。
@Html.ValidationMessageFor(model => model.VehicleModel.ManufacturerId)
上面將其綁定到VehicleModel_ManufacturerId
,其中:
@Html.DropDownList("ManufacturerId", String.Empty)
上面的將DropDown映射到了ManufacturerId
您需要更改一個或另一個以相互匹配。
2)在上面的代碼中,我沒有看到任何驗證屬性。 您在此處復制代碼時忘記了他們嗎?
希望對您有所幫助,如果您需要更多詳細信息,請告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.