Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 5 Html.BeginForm without model

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?

like image 779
stibay Avatar asked Sep 09 '14 13:09

stibay


People also ask

What is @using HTML BeginForm ()) in MVC?

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

What is the difference between Ajax BeginForm and HTML BeginForm?

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

What is the use of @using HTML BeginForm ())?

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.

Can we use multiple BeginForm in MVC?

Thanks for your help ! Multiple form tags should work fine in MVC unless they are nested.


1 Answers

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)
like image 159
mattytommo Avatar answered Oct 10 '22 21:10

mattytommo