简体   繁体   English

多种模型的MVC客户端验证

[英]MVC client-side validation for multiple models

I have three models: VehicleType , VehicleModel , and VehicleManufacturer . 我有三个模型: VehicleTypeVehicleModelVehicleManufacturer

Both VehicleType and VehicleManufacturer point to VehicleModel in the model, like so: 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; }

}

From there, VehicleModel points to the InventoryModel: 从那里,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; }
}

My problem is when I try to get client-side validation working on all three dropdownlists ( VehicleType , VehicleManufacturer , VehicleModel ), it only works with VehicleModel . 我的问题是,当我尝试在所有三个下拉列表( VehicleTypeVehicleManufacturerVehicleModel )上进行客户端验证时,它仅与VehicleModel一起VehicleModel

What needs to be done to validate these two dropdownlists using these models? 使用这些模型需要做些什么来验证这两个下拉列表?

Here is my controller (fyi): 这是我的控制器(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);
    }

View: 视图:

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

Please someone help. 请有人帮忙。 I've been on this for many, many hours! 我已经花了很多小时了!

There are actually two problems That I see above 我上面确实看到了两个问题

1) That you're not mapping the DropDownList and the ValidationMessageFor to the same model attribute. 1)您没有将DropDownList和ValidationMessageFor映射到同一模型属性。

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

The above is binding it to VehicleModel_ManufacturerId where as: 上面将其绑定到VehicleModel_ManufacturerId ,其中:

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

the above is mapping the DropDown to just ManufacturerId 上面的将DropDown映射到了ManufacturerId

You need to change one or the other to match each other. 您需要更改一个或另一个以相互匹配。

2) In the above code, I don't see any Validation Attributes. 2)在上面的代码中,我没有看到任何验证属性。 did you forgot them when you copied the code over here? 您在此处复制代码时忘记了他们吗?

Hope this helps, Let me know if you needed more details. 希望对您有所帮助,如果您需要更多详细信息,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM