2

I've read around that the given way of doing this seems to be having different view models (which is a bit overkill imo) for different actions/controllers.

I thought

@Html.EditorFor(model => model.Ingredient.Name, new { htmlAttributes = new { @class = "form-control", disabled = "disabled", data_val = "false"} })

might work but unfortunately not.

Does anyone have any ideas?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
rory
  • 1,490
  • 3
  • 22
  • 50
  • The main question here is: do you wish to disable the validation for the element or do you wish to make the item readonly? – Tom B. Jan 14 '16 at 13:04
  • @Tom B. Both if possible. Both are equally important. – rory Jan 14 '16 at 13:46
  • Possible duplicate of [ASP .NET MVC Disable Client Side Validation at Per-Field Level](https://stackoverflow.com/questions/5630424/asp-net-mvc-disable-client-side-validation-at-per-field-level) – KyleMit Nov 16 '17 at 17:37

4 Answers4

4

You can make use of .ignore to turn of the client validation on one or more elements.

The nice thing about this is that it is generic an can be applied to multiple elements by just adding the used ignore class.

Add this in your jquery code

$.validator.setDefaults({
    ignore: ".ignore"
});

Apply the ignore class on each element you whish to ignore/disable client side validation.

@Html.EditorFor(model => model.Ingredient.Name, new { htmlAttributes = new { @class = "form-control ignore", disabled = "disabled", data_val = "false"} })
Tom B.
  • 2,892
  • 3
  • 13
  • 34
  • 1
    Good solution. It's worth noting that the default value is [`ignore: ":hidden"`](https://github.com/jquery-validation/jquery-validation/blob/1.17.0/dist/jquery.validate.js#L285) so you might not want to override that entirely. You can probably set to `":hidden,.ignore" to ignore both, though I haven't tested it. – KyleMit Nov 16 '17 at 17:36
  • Worked for me. Excellent suggestion – Laurent Feb 22 '21 at 15:37
3

MVC can be funny when it comes to declaring elements as disabled. I used readonly instead to get around this issue. There just change disabled to readonly as shown below.

@Html.EditorFor(model => model.Ingredient.Name, new { htmlAttributes = new { @class = "form-control", readonly= "readonly", data_val = "false"} })

or an alternative is using an input html element

<input type="text" name="Ingredient.Name" id="Ingredient_Name" readonly="readonly" class="form-control, input-disabled" />

add css class

.input-disabled /* CHANGE STYLING OF READONLY FIELDS */
{
    background-color: #EEEEEE;    
}

then add your class to your html

@Html.EditorFor(model => model.Ingredient.Name, new { htmlAttributes = new { @class = "form-control, input-disabled", readonly= "readonly", data_val = "false"} })

I've created a the following to show you jsfiddle

Scanner
  • 597
  • 1
  • 9
  • 19
  • A **readonly editor** isn't an editor anymore. In terms to be "more correct" I should propose a displaytemplate. – Tom B. Jan 14 '16 at 12:59
  • 1
    If it doesn't need to be EditorFor then changing to TextBoxFor and setting @readonly = "readonly" is fine – Scanner Jan 14 '16 at 13:01
  • Rory what exactly is it you'd like to do with the element? – Scanner Jan 14 '16 at 13:36
  • @Scanner. Its an edit view. The element is the title/name of the Ingredient. I want to display it like the rest of the form but greyed out. It's no big deal, I can just display it as a label. I just thought that there'd be an easy way to exclude validation on a property – rory Jan 14 '16 at 13:45
  • @rory, all you need then is css. See my updated edit – Scanner Jan 14 '16 at 13:50
  • @rory, this can also be used on the input, which I've updated as well – Scanner Jan 14 '16 at 14:10
  • @Scanner - sorry, I should've said what I want to do is make it readonly AND exclude it from the validation – rory Jan 14 '16 at 14:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/100674/discussion-between-scanner-and-rory). – Scanner Jan 14 '16 at 14:14
2

All unobtrusive validation (the kind used by ASP.NET MVC) is predicated on finding and parsing data attributes on each object. Those carry all the information that the validation engine needs to run and only elements that have data-val="true" are included.

Any HTML attributes you pass will override any of the default attributes, so the easiest way is to just force data-val="false" and the form element won't be included in the validation engine and none of the other dependent tags will be parsed.

Here's an example from ASP .NET MVC Disable Client Side Validation at Per-Field Level:

@Html.TextBoxFor(m => m.BatchId, new { data_val = "false" })
KyleMit
  • 30,350
  • 66
  • 462
  • 664
0

You can disable client side validation in the Razor view before you render field and re-enable. I believe your question has already been answered here

Community
  • 1
  • 1
Kevin
  • 13
  • 3