ASP.NET MVC 4: Skip Validation in Child Object
asp.net-mvc-4, c#, model
Solution
You need to use View Models. Your view model should contain all the fields that you need on the view, minus Father property. Then in your controller you'll need to map view model to your model. You can easily do this with Automapper. (However, direct mapping from view to domain model is not recommended, but that you'll sort out later)
I know, at first this might look like a drag, but trust me. Your view is not the same as your domain model. Next thing you'll know, you'll need some sort of drop-down on your view and some other extra checkbox that you don't have in your domain model. Also, if you use view model, your security will be improved. I'll get you some info about that if interested.
Problem
First of all, sorry about my English. Not my natural language. I have a class named `Category` like the code below. Note that in this code I also have a `Category` property, in which I can reference a father Category. It's just the same class declared inside the class. Like Inception. So, this Father Category object has the same properties of "the class that declared him." The property `Name` is Required. Remember this property. ``` public class Category { public int? Id{ get; set; } [DisplayName("Father Category")] //NOT REQUIRED public Category Father { get; set; } [DisplayName("Category")] [Required(ErrorMessage = "Name is required")] //REMEMBER THIS REQUIRED PROPERTY!! public string Name { get; set; } [DisplayName("Status")] public bool Status { get; set; } [DisplayName("Description")] public string Description { get; set; } } ``` That's my Class. So, in the Category View, I can do something like this: Note: `CompleteEditorFor` and `CompleteDropDownListFor` are extended methods which add some extra html in each field, just for adjusting the layout. ``` @using (Html.BeginForm(null, null, FormMethod.Post)) { @Html.CompleteEditorFor(x => x.Name) @Html.CompleteEditorFor(x => x.Description) @Html.CompleteEditorFor(x => x.Status) @Html.CompleteDropDownListFor(x => x.Father.Id, ViewData["Categories"], "Id", "Name", "Select...") @Html.SubmitButton() } ``` The code above runs just fine. Now there's the problem: When I click the Save button, it makes a `HttpPost`, and this is the `Action` for that: (The code below has some modified message strings and extended methods.) (`CategoriesBLL` is the class that gets the categories from Database.) ``` [HttpPost] public ActionResult New(Category item) { ViewData.Add("Categories", CategoriesBLL.select()); try { if (ModelState.IsValid)//PROBLEM IS HERE!!!!!!!!! { if (CategoryBLL.insert(item) != 0) { ViewData.AddSuccess("Some Success Message"); return View(new Category()); } else { ModelState.AddError("Some error message"); return View(item); } } else { ModelState.AddError("Some SERIOUS error message"); return View(item); } } catch (Exception ex) { ModelState.AddError("Some EVEN MORE SERIOUS message"); return View(item); } } ``` The problem is at the `if (ModelState.IsValid)` line. Why? Because my `Category` class has a required property called `Name`. I don't need this property to do what I'm doing, I just need the `Id` of the `Father` property. I can get this `Id` in this line on the View: ``` @Html.CompleteDropDownListFor(x => x.Father.Id, ViewData["Categories"], "Id", "Name", "Select...") ``` And that works. But the `Name` property is `null`, and is Required, but it's only Required when I'm informing the Child class. Not the Father class. I don't even know how to search for it on Google or StackOverflow... Can anyone help me?