DonHoddinott2
asked on
MVC client-side validation With multiple models
I have three models: VehicleType, VehicleModel, and VehicleManufacturer.
Both VehicleType and VehicleManufacturer point to VehicleModel in the model, like so:
From there, VehicleModel points to the InventoryModel:
My problem is when I try to get client-side validation working on all three dropdownlists (VehicleType, VehicleManufacturer, VehicleModel), it only works with VehicleModel. I believe this is because in the View, I am calling the Inventory Model(@model IEnumerable<Dealership.Mod els.Invent ory>), and VehicleModel has a navigation property pointing to the Inventory model. But VehicleManufacturer and VehicleType point to the VehicleModel, so I don't see why all three models can't be validated.
What needs to be done to validate these two dropdownlists using these models?
Here is my controller (fyi)
And my View:
Thanks in advance!
Both VehicleType and VehicleManufacturer point to VehicleModel in the model, like so:
namespace Dealership.Models
{
public class VehicleModel
{
[Key]
public int ModelId { get; set; }
public int TypeId { get; set; }
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:
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. I believe this is because in the View, I am calling the Inventory Model(@model IEnumerable<Dealership.Mod
What needs to be done to validate these two dropdownlists using these models?
Here is my controller (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);
}
And my 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>
Thanks in advance!
ASKER
Hi Mishu,
I appreciate your answer, but I don't it is necessary to use IValidateObject in this instance. I want to understand why it isn't working, just as much as I want it resolved. Adding the [Required] attribute didn't work.
I appreciate your answer, but I don't it is necessary to use IValidateObject in this instance. I want to understand why it isn't working, just as much as I want it resolved. Adding the [Required] attribute didn't work.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try decorating the properties with [Required] attribute. You can also implement in your model the interface "IValidatableObject" to provice custom validation.
Regards,
Mishu