DanCaparroz
DanCaparroz

Reputation: 820

Ignoring an required field of an entity

I got the following variable into my entity:

[DataType(DataType.Currency)]   
[DisplayName("Value U$:")]
[Required(ErrorMessage = "Currency Required.")]
public decimal? CurrecyValue { get; set; }

Actually Im using this entity and I dont need this field. As soon as I post any information the ModelState becomes invalid because its required.

I know that I can use ModelState.Clear(); but, doing this I'll ignore all the other validations that I need.

Is there any way to just ignore this specific field without clearing my whole ModelState ?

Thanks !

Upvotes: 1

Views: 1187

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

Ugly and totally not recommended workaround:

ModelState.Remove("CurrecyValue");

Recommended solution:

Use view models. But real view models. Not some hybrids which you call view models and into into which you stick your domain entities and which you wonder how to get rid of simply because they are not adapted to the requirements of the given view. You should define a specific view model for each of your views. If you don't follow this very simple rule you will struggle a lot with ASP.NET MVC.

Upvotes: 5

Related Questions