0

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?

CesarMiguel
  • 3,756
  • 4
  • 24
  • 34

2 Answers2

0

Remove Required attribute from your model

Nilesh Gajare
  • 6,302
  • 3
  • 42
  • 73
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