-2

If I have a page with form elements like name, birthdate, address, etc and I'm updating it and saving the info with an JQuery Ajax call in javascript, do I need to have a

@using (Html.BeginForm("Edit", "Student", FormMethod.Post, new {@class = "form-horizontal", role = "form", enctype = "multipart/form-data"}))
{}

ASP.Net razor form around the fields? I have some required attributes on my view model that look for things like required fields and field lengths. Will these still get validation if I remove the form? I'm a little confused between submitting a razor form and submitting through javascript.

chuckd
  • 13,460
  • 29
  • 152
  • 331

1 Answers1

1

Mostly, what the Html.BeginForm does is add a <form> element around the markup the rest of the helpers generate. You don't get the data attributes for hooking into the Unobtrusive Validation script if you don't use the form wrapper.

... submitting a razor form1 and submitting through javascript2

The former (1) is just a plain ol' HTTP request (usually POST or GET). There's nothing special about how a form generated with the helper works. The latter (2) is something you can choose to do, and really has very little to do with the HtmlHelper. You can use the overloads of BeginForm to add attributes for hooking into the form with Javascript, but you're not required to use the helper - code the markup for the form yourself if it makes more sense.

Community
  • 1
  • 1
Tieson T.
  • 20,774
  • 6
  • 77
  • 92
  • maybe I don't understand something. Lets say I have no form, I'm submitting all my data through a javascript JQuery Ajax post call, will I still get validation on the fields that have attributes defined in my viewmodel (ex. [Required] public string name {get; set'}) – chuckd Sep 16 '15 at 04:11
  • @user1186050 Yes. The validation I was referring to the **client-side** validation. The validation you are referring to is server-side. Those bits could care less where the "form" comes from, they just want a set of form variables with the right names. – Tieson T. Sep 16 '15 at 04:13
  • Then you need to use the BeginForm helper, or apply the rules using the validation library yourself (which is a lot of work if you have a larg(ish) model). – Tieson T. Sep 16 '15 at 04:18
  • let me just make sure I understand what you are saying! Right now I have html.beginform that submits with a full postback to the server that refreshes the page. I have both client-side and server-side validation working. Now I want to use a ajax call to do the submitting but I still want to have client-side and server-side validation. Do I need to keep my html.beginform, remove it, use ajax.beginform, use neither and just call it from javascript? – chuckd Sep 16 '15 at 04:33
  • You can use the `Ajax.BeginForm()` if you also want to use the Unobtrusive Ajax scripts (I never have). It's generally easiest to take your working form and add jQuery hooks to capture the normal submit event, like I showed someone to do [here](http://stackoverflow.com/a/32555021/534109). Assuming you want to validate, you call the .validate() function in your event handler. and then check .valid() before performing your ajax call. If you need help with that, I suggest trying it yourself first, and then asking a new question. – Tieson T. Sep 16 '15 at 04:38
  • this is exactly what I wanted to know. thanks! I just tried it. It just seems unnecessary to have a html.beginform if I'm making an ajax call to post some data. Because now I have to say 'event'preventDefault' – chuckd Sep 16 '15 at 04:43