Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add data- attributes to Html.BeginForm

I use the following to create a form to upload images on a mobile site.

@using (Html.BeginForm("Form/", "Quote", FormMethod.Post, new { enctype = "multipart/form-data" })) 

However as it is using jQuery mobile, I have enabled Ajax so that transition between pages is nice and smooth. This has caused the problem that my form won't upload the images as you cannot do file uploads with ajax. I need to add the attribute data-ajax="false" to this form in order for it to allow my file uploads.

Does anyone know how I do this as I tried multiple variations of the following but couldn't get it to work:

@using (Html.BeginForm("Form/", "Quote", FormMethod.Post, new { enctype = "multipart/form-data", "data-ajax" = "false" })) 
like image 703
Pete Avatar asked Jul 23 '13 13:07

Pete


People also ask

What is HTML BeginForm ()?

"BeginForm()" is an extension method that writes an opening "<form>" tag to the response. "BeginForm()" is an extension method for both HtmlHelper and AjaxHelper classes.

What is the difference between Ajax BeginForm and HTML BeginForm?

Html. BeginForm() will create a form on the page that submits its values to the server as a synchronous HTTP request, refreshing the entire page in the process. Ajax. BeginForm() creates a form that submits its values using an asynchronous ajax request.


2 Answers

The trick is to use the underscore instead of the hyphen:

new { enctype = "multipart/form-data", data_ajax = "false" } 

The hyphen is not allowed as part of a c# identifier. The MVC framework translates the underscore automatically.

like image 166
Jan Avatar answered Sep 17 '22 22:09

Jan


You can use another overload:

@using (Html.BeginForm("Form", "Quote", FormMethod.Post, new Dictionary<string, object> { { "enctype", "multipart/form-data" }, { "data-ajax", "false"} }))  
like image 22
YD1m Avatar answered Sep 16 '22 22:09

YD1m