2008-10-19 191 views

回答

231

作为htmlAttributes的一部分,

Html.BeginForm(
    action, controller, FormMethod.Post, new { enctype="multipart/form-data"}) 

或者你也可以通过null行动和控制,以获得相同的默认目标为BeginForm()不带任何参数:

Html.BeginForm(
    null, null, FormMethod.Post, new { enctype="multipart/form-data"}) 
+43

正如一个音符,你可以通过空行动和控制,以获得相同的默认目标是BeginForm()不带参数给出。 – 2010-09-13 13:26:41

+2

@Brad:好评!将它合并到污水管中。 – chiccodoro 2011-10-21 11:15:59

+3

希望使用命名参数为此工作! – 2013-02-06 23:12:20

18

您也可以使用强类型版本的语法如下:

<% using (Html.BeginForm<SomeController>(x=> x.SomeAction(), 
      FormMethod.Post, 
      new { enctype = "multipart/form-data" })) 
    { %> 
12

我知道这是旧的,但如果你需要一遍又一遍地创建表格你可以创建一个自定义扩展:

public static MvcForm BeginMultipartForm(this HtmlHelper htmlHelper) 
{ 
    return htmlHelper.BeginForm(null, null, FormMethod.Post, 
    new Dictionary<string, object>() { { "enctype", "multipart/form-data" } }); 
} 

用法然后就变成

<% using(Html.BeginMultipartForm()) { %>