📅  最后修改于: 2023-12-03 15:01:18.293000             🧑  作者: Mango
Html.BeginForm
is a C# method that returns an opening form
tag with specified attributes. This method is part of the System.Web.Mvc.Html
namespace and is used in ASP.NET MVC applications to create HTML forms.
public static MvcForm BeginForm(
string actionName,
string controllerName,
object routeValues,
FormMethod method,
object htmlAttributes
)
actionName
: A string that represents the name of the action method to submit the form to.
controllerName
: A string that represents the name of the controller to submit the form to.
routeValues
: An object that contains the parameters for the action method.
method
: An enumeration that represents the HTTP method to use for the form submission.
htmlAttributes
: An anonymous object that contains the HTML attributes to add to the form
tag.
@using (Html.BeginForm("Create", "Home", FormMethod.Post)) {
<div>
@Html.LabelFor(model => model.Name)
@Html.TextBoxFor(model => model.Name)
</div>
<div>
@Html.LabelFor(model => model.Qty)
@Html.TextBoxFor(model => model.Qty)
</div>
<input type="submit" value="Submit" />
}
In this example, we’re creating an HTML form using Html.BeginForm
. With the FormMethod.Post
parameter, the form will be submitted via "POST" method. We provide the actionName
and controllerName
parameters to specify where the form data will be submitted.
Inside the form, we create two input fields using the @Html.TextBoxFor
helper method, and a submit button.
Html.BeginForm
is a useful method in ASP.NET MVC for quickly creating HTML forms with minimal HTML coding required. By specifying the action and controller parameters, we can easily control where the form data will go. The anonymous object parameter gives us great flexibility in adding custom attributes to the form tag.