dimanche 16 octobre 2016

HTML.BeginForm pass in arugment

I am new to MVC. A bit late to the game but I am trying to learn. I am sure this has been asked, but I am having trouble.

I want the form to post to


For example:


The {argument} is the value of the textbox.

I have the following in my shared _Layout.chtml. The view is not bound to any Model as it will be a text field for searching.

<ul class="nav navbar-nav navbar-right">
      @using (@Html.BeginForm("Index", "Search",  FormMethod.Get, new { @class = "navbar-form navbar-right" }))
      <div class="input-group col-md-12 col-sm-12">                                
         @Html.TextBox("phrase", (String)ViewBag.Message, new { placeholder = "Search", @class = "form-control" })
         <span class="input-group-btn">
           <button class="btn btn-danger" type="button">
             <span class="glyphicon glyphicon-search"></span>

Here is my controller:

public class SearchController : Controller
        // GET: /search/index/{argument}
        // example = http://localhost:53967/Search/index/foobar
        public ActionResult Index(String phrase)
            String _phrase = phrase.Encode();

            ViewBag.Message = _phrase;

            return View();

        [HttpPost] // post back
        public ActionResult Index(SearchModel model) {

            return View();

Aucun commentaire:

Enregistrer un commentaire