- Hi Guys,
I have a very simple form which looks like the following:
TypeScript@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:
C#public class SearchController : Controller { [HttpPost] public ActionResult FullSearch(SearchViewModel SearchTerms) { return View("SearchResults", SearchTerms); } }The model looks like this:
C#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...?
Many thanks
Model is Null
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Msomi Thabiso X4 pointsPosted Jul 29, 2015, 7:18 PM EDT
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:
public class SearchController : Controller { [HttpPost] public ActionResult FullSearch(SearchViewModel Model) { return View("SearchResults", Model); } }Cheers
1 short reply
Julias Nyerere2 pointsPosted Jul 29, 2015, 7:21 PM EDT