I have Edit Action with Html.BeginForm
. How can I add HTML attributes?
I know only one way:
@using (Html.BeginForm(\"Edit\", \"Clients\"
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))
{
}
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" })
If this might be helpful for some people, this works for me:
@using (Html.BeginForm("RefreshData", "Home", FormMethod.Post,
new { Id = "timerangeId", @name = "timerange" }))
{
// form elements and input
}
In Javascript:
document.getElementById("timerangeId").submit();