How would I go about adding enctype="multipart/form-data" to a form that is generated by using <% Html.BeginForm(); %>?
3 Answers
As part of htmlAttributes,e.g.
Html.BeginForm(
action, controller, FormMethod.Post, new { enctype="multipart/form-data"})
Or you can pass null for action and controller to get the same default target as for BeginForm() without any parameters:
Html.BeginForm(
null, null, FormMethod.Post, new { enctype="multipart/form-data"})
2 Comments
Govind
hi, how do i specify enctype as Shift-JIS which is japanese encoding format?
César León
I always prefer to specify the action / controller, because the url can be manipulated depending on what you page do, so letting action / controller on null may cause unexpected behaviors.
You can also use the following syntax for the strongly typed version:
<% using (Html.BeginForm<SomeController>(x=> x.SomeAction(),
FormMethod.Post,
new { enctype = "multipart/form-data" }))
{ %>
4 Comments
Jason Bunting
Can't do this in the current version (RC1).
Pure.Krome
Which is a massive bummer :( So how can we do this? do we need another dll? MVC futures or something?
dp.
Yes, indeed...I believe all of the strongly typed (expression-based) methods are in the futures assembly (aspnet.codeplex.com/Release/…).
chiccodoro
@Jason, dp: Using Nick's extension method, it would be possible to provide that kind of signature, too. Still including futures is certainly a better approach.
I know this is old but you could create a custom extension if you needed to create that form over and over:
public static MvcForm BeginMultipartForm(this HtmlHelper htmlHelper)
{
return htmlHelper.BeginForm(null, null, FormMethod.Post,
new Dictionary<string, object>() { { "enctype", "multipart/form-data" } });
}
Usage then just becomes
<% using(Html.BeginMultipartForm()) { %>