Skip to content

Model is Null

Julias Nyerere

Asked Jul 29, 2015, 6:30 PM EDT2 answers
Share
LinkedInX

  • 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

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • +1

     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:

    C#
    public class SearchController : Controller { [HttpPost] public ActionResult FullSearch(SearchViewModel Model) { return View("SearchResults", Model); } }

    Cheers

    +1
  • 1 short reply
    • 0
      Thanks a Lot man It works
      0