I'm working in ASP .NET MVC 4 and I'm trying to validate a field as required in a specific form, but in the database the field is not required. Is it possible to do this with ValidationMessageFor or will do I have to do in Javascript?
Asked
Active
Viewed 85 times
2 Answers
0
Remove Required
attribute from your model

Nilesh Gajare
- 6,302
- 3
- 42
- 73
-
In model it is not required, but in specific form i want it do – CesarMiguel Jan 08 '14 at 12:04
-
check it using javascript..whether value is null or not before submitting – Nilesh Gajare Jan 08 '14 at 12:04
-
I thought so, but could be any way to do so and easier – CesarMiguel Jan 08 '14 at 12:06
-
i think...javascript would be easier to do – Nilesh Gajare Jan 08 '14 at 12:07
0
It seems you are employing just one model for view and for business domain/data access as well. I assume your application is small enough to live up with this.
When it becomes larger enough, more impedance mismatch could come up. It is recommended then to separate your model at least for views and business domain/data access. There are good tools to automate obvious conversions like Automapper.
Updated (as it both relates to view models):
The problem is known and has several approaches, from which I prefer most this one:
@Html.TexBoxFor(model => model.YourField,
new Dictionary<string, object> { { "data-val", false }})
as it employs ASP.NET MVC helpers and you are free from maintaining additional js code

Community
- 1
- 1

Marcin Wachulski
- 567
- 4
- 14
-
I'm just using the same entity to different forms. And there is a specific form that a certain field is required. I've done the validations in javascript, thanks anyway – CesarMiguel Jan 09 '14 at 11:01