Reputation: 2702
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?
Upvotes: 4
Views: 6229
Reputation: 1485
ModelState.Remove("PropertyNameInModel");
or
ModelState.Remove<ViewModel>(x => x.SomeProperty);
Upvotes: 1
Reputation: 35106
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.
Upvotes: 2
Reputation: 4161
public class Category : IValidatableObject
{
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; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var results = new List<ValidationResult>();
if (this.Enable)
{
Validator.TryValidateProperty(this.Name ,
new ValidationContext(this, null, null) { MemberName = "Name" },
results);
Validator.TryValidateProperty(this.Status,
new ValidationContext(this, null, null) { MemberName = "Status" },
results);
}
return results;
}
}
public void Validate()
{
var toValidate = new Category()
{
Name = "Just a name",
Status = true
};
bool validateAllProperties = false;
var results = new List<ValidationResult>();
bool isValid = Validator.TryValidateObject(
toValidate,
new ValidationContext(toValidate, null, null),
results,
validateAllProperties);
}
Upvotes: 1
Reputation: 3153
You may want to consider making the class IValidatableObject interface on your model instead of property decorators. If you really want to use a property decorator then you might hav eto right a custom one.
Upvotes: 2