0

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?

1
  • Your using the wrong overload - you need this one Commented Aug 23, 2016 at 23:12

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way to not specify the FormMethod? I am doing post and get with $.ajax()
There are only 2 methods, GET or POST. What problem are you having when you explicitly set the FormMethod ? It should not give you any trouble with your ajax calls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.