I have a simple search form on my _Layout page.
How can I easily pass the value from search-fld to the controller? Without having to create a Model or ViewModel.
@using (Html.BeginForm("Search", "Home", new { id = "search-fld" }, FormMethod.Post, new { @class = "header-search pull-right" }))
{
@Html.AntiForgeryToken()
<input type="text" placeholder="Search" id="search-fld">
<button type="submit">
<i class="fa fa-search"></i>
</button>
}
If I run this then "search-fld" get send to the controller (offcourse)
Use Ajax form instead and get the value with Jquery?
"BeginForm()" is an extension method for both HtmlHelper and AjaxHelper classes. It returns an MVCForm object from both HtmlHelper and AjaxHelper class instances so there is not much difference but the AjaxHelper method submits the form asynchronously using JavaScript.
@Html. BeginForm is used to post the data by full page refresh whereas @Ajax. BeginForm performs Post back function and allows some portion of Html to be reloaded rather than overall page refresh.
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. BeginForm extension method in ASP.NET MVC5.
Thanks for your help ! Multiple form tags should work fine in MVC unless they are nested.
Simply by giving your input
a name
attribute:
<input type="text" placeholder="Search" id="search-fld" name="searchValue">
Then matching that name with a parameter in your controller HttpPost
method:
[HttpPost]
public ActionResult Search(string searchValue)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With