In our application, we have a Domain layer that contains classes with DataAnnotations for validation.
We use these classes in our models at our ASP.NET MVC ui level.
For instance:
Domain Level:
public class Company { public int Id { get; set; } [Required] [StringLength(50)] public string Description { get; set; } // ... some model logic abreviated } public class Supplier { public int Id { get; set; } [Required] public string Name { get; set; } [Required] public Company Company { get; set; } // ... some model logic abreviated }
In our ASP.NET MVC presentation layer:
public class SupplierEditModel { public Supplier Supplier { get; set; } public IEnumerable<Company> Company { get; set; } public int SelectedCompany { get; set; }
In this case, we have a page with DropDownList companies. The list has a binding:
@Html.DropDownListFor(m => m.SelectedCompany, new SelectList(Model.Companies, "Id", "Description", Model.SelectedCompany))
Our problem is the POST method of our controller, when we check ModelState.IsValid, the model is invalid because the provider. The company is NULL. Then we can get the company using SelectedCompany, but our problem is that it means that we cannot do something like this:
[HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(SupplierEditModel model) { if (ModelState.IsValid) { model.CreateSupplier(_supplierService); return RedirectToAction("Index"); } return RedirectToAction("Create"); }
We would like to use verification before creating a supplier.
source share