我有一个以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.
更多推荐
发布评论