带有html属性的html.BeginForm asp.net mvc4

我有Html.BeginForm编辑动作。 如何添加HTML属性?

我只知道一种方式:

 @using (Html.BeginForm("Edit", "Clients", FormMethod.Post, new { @class="example"})) { } 

但如果我使用这种方法,我无法传递当前的ID

是否可以在不修改操作URL的情况下将HTML属性添加到表单中?

您需要的覆盖是:

 @using( Html.BeginForm("Edit", "Clients", new { Id=Model.Id}, FormMethod.Post, new { @class = "example" } ) ) { } 
  • 像“id”这样的路由值作为第三个参数传递。
  • 像“class”这样的HTML属性作为第五个参数传递。

请参阅MSDN文档。

Action和Controller参数也可以为null以使用默认操作:

 Html.BeginForm( null, null, FormMethod.Post, new { id=”formname”, @class="formclass" }) 

通过ControllerA的ActionLink调用

 @using (Html.BeginForm("Create", "StudentPChoice", new { StudentPChoiceId = Model.StudentPChoiceId }, FormMethod.Post)) { } 

要么

 @using (Html.BeginForm("Create", "ControllerB", new { ControllerBId = Model.ControllerAId }, FormMethod.Post)) { }