多个模型的MVC客户端验证

时间:2014-02-13 19:43:27

标签: c# asp.net-mvc asp.net-mvc-4 asp.net-mvc-5

我有三个模型:VehicleTypeVehicleModelVehicleManufacturer

模型中VehicleTypeVehicleManufacturer都指向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; }
}

我的问题是,当我尝试对所有三个下拉列表(VehicleTypeVehicleManufacturerVehicleModel)进行客户端验证时,它只适用于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 个答案:

答案 0 :(得分:1)

我上面看到的实际上有两个问题

1)您没有将DropDownList和ValidationMessageFor映射到相同的模型属性。

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

上述内容将其绑定到VehicleModel_ManufacturerId,其中:

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

以上是将DropDown映射到ManufacturerId

您需要更改其中一个以相互匹配。

2)在上面的代码中,我没有看到任何验证属性。当你在这里复制代码时,你忘记了它们吗?

希望这会有所帮助,如果您需要更多详细信息,请与我们联系。