Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 4 Form - submit button doesn't do anything

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; }
}
like image 368
user1025852 Avatar asked Sep 08 '26 10:09

user1025852


2 Answers

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"/>
}
like image 138
Andrey Gubal Avatar answered Sep 12 '26 14:09

Andrey Gubal


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>
 }
like image 38
German Blanco Avatar answered Sep 12 '26 14:09

German Blanco