AndreaNobili
AndreaNobili

Reputation: 43077

Some doubts about Html.BeginForm work in .cshtml view?

I am pretty new in C# (I came from Java) and I have the following doubt about how .NET handle forms in .cshtml file.

In a view named Index.xshtml I have something like it:

@using (Html.BeginForm("Index", "Vulnerability", FormMethod.Post, new { id = "MyForm" }))
{

    <div class="ui-field-contain">
        <label for="Filter_CVE">CVE:</label>
        <input type ="text" data-mini="true" data-clear-btn="true" id="Filter_CVE" name="Filter.CVE"  value="@Model.Filter.CVE"   />
    </div>   

    <div data-role="controlgrup" data-type="horizontal" data-mini="true">
        <input type="reset" data-inline="true" data-mini="true" value="Reset" />
        <input type="submit" data-inline="true" data-mini="true" value="Seach" data-icon="search" />
    </div>
}

Now it is pretty clear for me that this code create a form and 2 buttons. My doubt is related tho this line

@using (Html.BeginForm("Index", "Vulnerability", FormMethod.Post, new { id = "MyForm" }))

Looking to the official documentation (here: http://msdn.microsoft.com/en-us/library/system.web.mvc.html.formextensions.beginform%28v=vs.118%29.aspx) I can't find my situation.

So what is the meanining of the previous BeginForm method parameters?

I think that they could be:

Upvotes: 0

Views: 1145

Answers (3)

jao
jao

Reputation: 18620

Index: this is the name of your action

Vulnerability is the name of your controller

FormMethod.Post means the form is sent via POST

new { id = "MyForm" } are the html attributes, in this case the <form> tag wil get an ID of MyForm

Imagine you have the following code in your VulnerabilityController:

public ActionResult Index() {

} 

Your form will send all the data entered in the HTML input fields to the Index action (by POST)

Upvotes: 1

Christian Phillips
Christian Phillips

Reputation: 18769

Index: This is your View, and will be represented in the controller as an Action Method

Vulnerability: This is your controller

FormMethod.Post: This sets the form Method

new { id = "MyForm" }: This will set the id of the form, as in <form id="MyForm"...

Upvotes: 1

Florian F.
Florian F.

Reputation: 4700

There's a documentation for that.

Method signature :

public static MvcForm BeginForm(
    this HtmlHelper htmlHelper,
    string actionName,
    string controllerName,
    FormMethod method,
    Object htmlAttributes
)
  • actionName

Type: System.String The name of the action method.

  • controllerName

Type: System.String The name of the controller.

  • method

Type: System.Web.Mvc.FormMethod The HTTP method for processing the form, either GET or POST.

  • htmlAttributes

Type: System.Object An object that contains the HTML attributes to set for the element.

Upvotes: 3

Related Questions