无法使用Html.BeginForm为表单分配属性(Cannot assign attributes to form using Html.BeginForm)

编程入门 行业动态 更新时间:2024-10-27 04:37:55
无法使用Html.BeginForm为表单分配属性(Cannot assign attributes to form using Html.BeginForm)

我有一个以Html.BeginForm()辅助方法Html.BeginForm()的表单:

@using (Html.BeginForm( null,null, new { @id = "MaintenanceForm", @class = "datatable", @nonvalidate="nonvalidate" } ))

并将表单呈现为:

<form action="(controller's name)/(current action's name)/MaintenanceForm?class=datatable" method="post">

未分配id,class和nonvalidate等属性。 另外我不想要默认的Http方法。 我能做什么?

I have a form begun with the Html.BeginForm() helper method:

@using (Html.BeginForm( null,null, new { @id = "MaintenanceForm", @class = "datatable", @nonvalidate="nonvalidate" } ))

and the form is rendered as:

<form action="(controller's name)/(current action's name)/MaintenanceForm?class=datatable" method="post">

The attributes such as id, class, and nonvalidate aren't assigned. Also I do not want a default Http Method. What can I do?

最满意答案

您当前的代码与BeginForm方法的下方重载匹配

public static MvcForm BeginForm( this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues )

这里的第三个参数是路径值的对象。 这些将作为查询字符串键值添加到表单的action属性值。 这就是您将这些大网址视为操作属性值的原因。

如果要指定html属性( Id,class等),请使用此重载,它具有第四个参数,该参数采用html属性。 第三个参数是FormMethod。

public static MvcForm BeginForm( this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method, object htmlAttributes )

这应该工作。

@using (Html.BeginForm("Create", "Post",FormMethod.Post, new { @id = "MaintenanceForm", @class = "datatable", @nonvalidate = "nonvalidate" })) { }

使用操作方法名称和控制器名称替换Create和Post 。

Your current code is matching with the below overload of BeginForm method

public static MvcForm BeginForm( this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues )

The third parameter here is an object for the route values. These will be added as the querystring key value(s) to your form's action attribute value. That is the reason you are seeing those big url as the action attribute value.

If you want to specify the html attributes( Id,class etc), Use this overload which has a fourth parameter which takes the html attributes. The third parameter is the FormMethod.

public static MvcForm BeginForm( this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method, object htmlAttributes )

This should work.

@using (Html.BeginForm("Create", "Post",FormMethod.Post, new { @id = "MaintenanceForm", @class = "datatable", @nonvalidate = "nonvalidate" })) { }

Replace Create and Post with your action method name and controller name.

更多推荐

本文发布于:2023-07-15 05:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110607.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   属性   分配   BeginForm   Html

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!