asp.net-mvc Html.BeginForm 并添加属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/216600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 22:48:05  来源:igfitidea点击:

Html.BeginForm and adding properties

asp.net-mvc

提问by KevinUK

How would I go about adding enctype="multipart/form-data"to a form that is generated by using <% Html.BeginForm(); %>?

我将如何添加enctype="multipart/form-data"到使用生成的表单<% Html.BeginForm(); %>

回答by liggett78

As part of htmlAttributes,e.g.

作为 htmlAttributes 的一部分,例如

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

Or you can pass nullfor action and controller to get the same default target as for BeginForm() without any parameters:

或者你可以传递null给 action 和 controller 以获得与 BeginForm() 相同的默认目标,而无需任何参数:

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

回答by dp.

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

回答by Nick Olsen

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()) { %>