3

Trying to implement a form in MVC4 (+Razor), but submit button is not doing anything.

Controller (that should get the post action):

public class GeneralController
{
    [HttpPost]
    public ActionResult SearchResults(SearchParamsModel searchParams)
    {
        // doin some stuff here
        return View("SearchResultsView");
    }
}

View (.cshtml)

@model Models.SearchParamsModel 
@using (Html.BeginForm("SearchResults", "General", FormMethod.Post))
{
    <section class="form-field">
        <input type="text" name="Property1" id="Property1" class="field field139 autocomplete-init-no-img" />
        <label for="Property1">value1</label>

        <form action="" method="post" class="clearfix">           
            <input type="submit" value="some value" class="submit btn blue-btn special-submit" />
        </form>
    </section>
}

Model

public class SearchParamsModel 
{
    public string Property1{ get; set; }
}
Bojan Kseneman
  • 15,488
  • 2
  • 54
  • 59
user1025852
  • 2,684
  • 11
  • 36
  • 58

3 Answers3

5

If you just need to implement searching you don't need to use ViewModel, you may send string with search request. And it shouldn't be Post method:

public ActionResult SearchResults(string searchString)
{
    //code for searching

    return View(yourmodel);
}

In View

@using (Html.BeginForm())
{     
    Searching: @Html.TextBox("SearchString")
    <input type="submit" value="Search"/>
}
Akshay Soam
  • 1,580
  • 3
  • 21
  • 39
Andrey Gubal
  • 3,481
  • 2
  • 18
  • 21
  • Thanks I'll try that. However I do want my code to be clean and my search has multiple input params, din't want to maintain search with plain strings.. – user1025852 Oct 04 '13 at 11:24
  • Thanks, I did what you suggested. However it doesn't work. Only if I add in the begining of my .cshtml @{ Layout = null} it works (to simplify things let's just say I have a button without the field and I want to reach the controller) – user1025852 Oct 08 '13 at 20:31
2

The Html.BeginForm helper will create the form tags for you, try it...

View:

@model Models.SearchParamsModel 

@using (Html.BeginForm("SearchResults", "General", FormMethod.Post))
 {
  <section class="form-field">
    <input type="text" name="Property1" id="Property1" class="field
                  field139 autocomplete-init-no-img" />
    <label for="Property1">value1</label>
    <input type="submit" value="some value" 
                    class="submit btn blue-btn special-submit" />   
  </section>
 }
German Blanco
  • 816
  • 6
  • 9
  • You have two forms nested, so the submit button only works for the internal form. You never submit the external form. The external form is created with the Html.BeginForm helper and never fired. – German Blanco Oct 04 '13 at 05:23
  • Thanks. Html.BeginForm should be exactly on top of
    ? anyway it doesn't do the trick...something is probably still missing here
    – user1025852 Oct 04 '13 at 05:38
  • Is the controller fired? if so, edit your controller to use FormCollection to pull form values by name like this http://stackoverflow.com/questions/6995285/how-to-access-my-formcollection-in-action-method-asp-net-mvc/16822103#16822103 – German Blanco Oct 04 '13 at 05:45
  • no the controller is not fired..first i want to debug the invoke (after that I'll work on presenting the results...) – user1025852 Oct 04 '13 at 06:05
1

if I do the same in MVC 4 or 5 I get the same result.

look at adding <fieldset> tags around all controls:

@using (Html.BeginForm("SearchResults", "General", FormMethod.Post))
{
  <fieldset>
  // your controls...
  <input type="text" name="Property1" id="Property1" class="field field139 autocomplete-init-no-img" />
  <label for="Property1">value1</label>
  <input type="submit" value="some value" class="submit btn blue-btn special-submit" />      
  // if you need a partial form included:
  @{Html.RenderPartial("_SomeOtherPartial", @Model);}
  // etc..
  </fieldset>
}

Give that a try...

Let me know

Robert Achmann
  • 1,986
  • 3
  • 40
  • 66