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

喜夏-厌秋 提交于 2019-12-02 17:53:54
Dejan.S

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!