Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does @Html.BeginForm() work? and search result in Microsoft ASP.Net MVC 5 tutorial?

I am working on MVC 5 Asp.Net and following this tutorial. I am wondering how the heck does this fetch the result when I click the Filter button?

There comes a point where this code is added in Movie/view/Index.cshtml

@using (Html.BeginForm())
{    
     <p> Title: @Html.TextBox("SearchString") <br />   
     <input type="submit" value="Filter" /></p> 
} 

Now as far as I know, it creates a textbox and a button on screen. But how is this button calling the search(index) function and passing the value of textbox in the function, I could not get this.

like image 583
mfs Avatar asked Oct 26 '14 14:10

mfs


People also ask

How does HTML BeginForm work?

In the "BeginForm()" method we passed an action name and controller name so that the action method will be called that handles the post data request. So it creates an action that returns the form data into the content to be shown on the UI on form submission. Now the process is ready and runs the application.

What is the use of HTML BeginForm in MVC?

Html. BeginForm is the Html Helper Extension Method that is used for creating and rendering the form in HTML. This method makes your job easier in creating form. Here, is the method to create a form using Html.

What is difference between HTML BeginForm and ajax BeginForm?

Html. 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.

What is HTML ActionLink in MVC?

Html. ActionLink creates a hyperlink on a view page and the user clicks it to navigate to a new URL. It does not link to a view directly, rather it links to a controller's action.


1 Answers

It's not a stupid question. @html.BeginForm() works like this. It has some parameters you could add to it like Action Controller FormType htmlAttributes. The way it works is that if you leave it empty it will look for a post action with the same name that on the page you are now, for example if you are in on the login page, it will look for a login post action. I always write what action and controller I want it to access.

@Html.BeginForm("AddUser", "Admin", FormMethod.Post, new { @class = "my_form"}) {

}

So your post action should accept parameters that your form contains, and that can be a Model ie a Product, ViewModel or single string parameters. In your case with the search your action should look like

[HttpPost]
public ActionResult Search(string SearchString) 
{
   //do something here
}

Please note here, for the search string to be passed into the method. The name of the <input> has to be the same as the parameter your action takes. So our form should be like this

@using (Html.BeginForm("Search", "YOUR CONTROLLER", FormMethod.Post)){    
     <p> Title: @Html.TextBox("SearchString") <br />   
     <input type="submit" value="Filter" /></p> 
} 

Hope this brings clarity.

like image 108
Dejan.S Avatar answered Oct 05 '22 07:10

Dejan.S