How would I go about adding enctype="multipart/form-data" to a form that is generated by using <% Html.BeginForm(); %>?
            Asked
            
        
        
            Active
            
        
            Viewed 1.3e+01k times
        
    143
            
            
         
    
    
        Konrad Rudolph
        
- 530,221
- 131
- 937
- 1,214
 
    
    
        KevinUK
        
- 5,053
- 5
- 33
- 49
3 Answers
259
            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"})
 
    
    
        chiccodoro
        
- 14,407
- 19
- 87
- 130
 
    
    
        liggett78
        
- 11,260
- 2
- 29
- 29
- 
                    hi, how do i specify enctype as Shift-JIS which is japanese encoding format? – Govind Jun 30 '14 at 16:02
- 
                    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. – César León Mar 08 '17 at 20:26
19
            
            
        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" })) 
   { %>
 
    
    
        Pure.Krome
        
- 84,693
- 113
- 396
- 647
 
    
    
        dp.
        
- 8,138
- 7
- 33
- 28
- 
                    1
- 
                    Which is a massive bummer :( So how can we do this? do we need another dll? MVC futures or something? – Pure.Krome Apr 25 '09 at 01:35
- 
                    4Yes, indeed...I believe all of the strongly typed (expression-based) methods are in the futures assembly (http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24471). – dp. Apr 25 '09 at 08:50
- 
                    @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. – chiccodoro Oct 21 '11 at 11:19
13
            
            
        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()) { %>
 
    
    
        SteveC
        
- 15,808
- 23
- 102
- 173
 
    
    
        Nick Olsen
        
- 6,299
- 11
- 53
- 75