Hello Guys,
I have a very simple form which looks like the following:
@model ContentAgent_Licensing_MVC.Models.Licensing.ViewModels.SearchViewModel @using (Html.BeginForm("FullSearch", "Search", FormMethod.Post)) { @Html.TextBoxFor(model => model.SearchTerms, new { @class = "control-textbox", @placeholder = "Search" }) <button type="submit"><img src="~/Content/images/search-icon.png" width="15" height="15" />button> }
The controller on the other end looks like this:
public class SearchController : Controller { [HttpPost] public ActionResult FullSearch(SearchViewModel SearchTerms) { return View("SearchResults", SearchTerms); } }
The model looks like this:
public class SearchViewModel { public string SearchTerms { get; set; } }
The problem is when I submit the form, the SearchViewModel object is always null when I debug into the FullSearch method. Does anyone know what I'm doing wrong...?
Nitin SontakkePosted Sep 15, 2016, 11:27 PM
Jade DonPosted Sep 15, 2016, 3:02 PM
Thabiso MsomiPosted Sep 15, 2016, 3:00 PM
I think you are calling the model in the controller "SearchTerms" which matches one of the properties in the model. This might have been confusing the binder. Just change SearchTerms to Model and it might work. So the amended code will be:
Cheers