How do I add data- attributes to Html.BeginForm

醉酒当歌 提交于 2019-12-03 14:33:35

问题


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" }))

回答1:


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.




回答2:


You can use another overload:

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


来源:https://stackoverflow.com/questions/17811651/how-do-i-add-data-attributes-to-html-beginform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!