1

Could someone provide a good tutorial on how may I validate my html fields with unobtrusive, but without using MVC helpers. Is it possible at all?

I would like to keep the plain html and use input type="text" field instead of mvc helpers <%= Html.TextBox("Name") %> but still using the model validation..

public class Employee
{
    [Required]
    public string Name { get; set; }
}

Also is it possible with jquery Ajax?

Thanks

tereško
  • 58,060
  • 25
  • 98
  • 150
Alex
  • 249
  • 1
  • 4
  • 17
  • Do you have a good reason for doing that? – ataravati Jul 25 '13 at 23:30
  • Reason 1. Yes, I do not want to stick to Microsoft, maybe I will decide to move at some stage. Reason 2. Let's say if tomorrow asp.net MVC will die like the forms dying today, I will need to move to another environment even if it microsoft, they will change all the logic and helpers/controls again, and then I will need to overwrite everything from scratch, why should I do it If I can use plain html and javascript for the client which always will be the same? – Alex Aug 05 '13 at 02:18
  • Well, that's like saying I don't want to write my code in c#, because I may want to use java in the future, and then I'll have to start everything from scratch. – ataravati Aug 05 '13 at 03:18
  • Why should I rewrite both client and server if I can rewrite only 1 of them? – Alex Aug 05 '13 at 04:14

1 Answers1

1

While it is possible to perform unobtrusive validation in MVC without using helpers, it will be painfully for you as developer to do it manually without usage of mvc helpers. Saying strictly, it will kill your performance and make your code unreadable.

Basically, unobtrusive validation consists of two parts: server side, via data-attributes to your model fields and MVC Helpers, which generate necessary markup, and client side library, jquery.validate.unobtrusive.js, which parses those markup to meaningful parts for jquery validate plugin.

So, in general, you can manually write necessary markup, as long as validation js library will be loaded, validation will work. For example, field, which is subject of validation, must be marked with data-val='true' attribute. If you want to make your field required, you should write additionally something like data-val-required="error message". For length validation - data-val-length-min="5" (obviously, minimum) data-val-length-max="50" (maximum length), data-val-length="Min 5 max 50 chars required".

While, when using normal mvc approach, it is just a question of model attribute:

[Required]
[StringLength(50, MinimumLength = 5)]
public string Name { get; set; }

and one line of code in markup:

@Html.TextBoxFor(o=>o.Name)

Nice, shiny, separates View and Model, and helps KISS.

And for second part of your question. If I understood your problem correctly, you want to validate dynamic forms. Probably it will be answer: jquery.validate.unobtrusive not working with dynamic injected elements

Community
  • 1
  • 1
Dmytro
  • 1,590
  • 14
  • 14
  • 1
    That's annoying, i do want to keep my html clean without any helpers... If at some stage I will need to move from asp.net to other technology I will do a lot of work to clean all these asp code even in client... – Alex Aug 02 '13 at 01:08