39

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" }))
2
  • Could you try @data-ajax = "false", please? Commented Jul 23, 2013 at 13:41
  • @AndreCalil I tried that as one of my many variations Commented Jul 23, 2013 at 13:43

2 Answers 2

73

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this - I was thinking to come up with a way to translate _ to - but thought I'd do a quick search for the "right" way first. Razor engine, you do love me!
21

You can use another overload:

@using (Html.BeginForm("Form", "Quote", FormMethod.Post, new Dictionary<string, object> { { "enctype", "multipart/form-data" }, { "data-ajax", "false"} })) 

1 Comment

while there is more junk { } in this one, it works great for Vue if you need to add a ref attribute to a form since ref is a keyword in C# and cannot work for the "object htmlAttributes" overload.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.