I have Edit Action with Html.BeginForm
. How can I add HTML attributes?
I know only one way:
@using (Html.BeginForm("Edit", "Clients", FormMethod.Post, new { @class="example"})) {
}
but if I use this method I cannot pass current ID
Is it possible to add HTML attributes to form without modifying action URL?
"BeginForm()" is an extension method that writes an opening "<form>" tag to the response. "BeginForm()" is an extension method for both HtmlHelper and AjaxHelper classes.
BeginForm() will create a form on the page that submits its values to the server as a synchronous HTTP request, refreshing the entire page in the process. Ajax. BeginForm() creates a form that submits its values using an asynchronous ajax request.
The Html. BeginForm helper method contains a couple overloads whose intended purpose is to make writing routed forms easier. It is aware of MVC stucture and makes sure its targeting a controller and action.
The override you need is:
@using( Html.BeginForm("Edit", "Clients", new { Id=Model.Id}, FormMethod.Post, new { @class = "example" } ) ) { }
See MSDN docs.
The Action and Controller parameters can also be null to use the default action:
Html.BeginForm( null, null, FormMethod.Post, new { id=”formname”, @class="formclass" })
Calling via an ActionLink from ControllerA
@using (Html.BeginForm("Create",
"StudentPChoice",
new { StudentPChoiceId = Model.StudentPChoiceId },
FormMethod.Post))
{
}
OR
@using (Html.BeginForm("Create",
"ControllerB",
new { ControllerBId = Model.ControllerAId },
FormMethod.Post))
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With