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; }
}
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"/>
}
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>
}
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