Why is my MVC5 Post Action model not populating

I have one model in my solution that is not populating in my POST form action. Why is this model different?

Here is a model that won't link in my post:

public class CompanyListingParameters
{
    public String Category { get; set; }
    public String CityState { get; set; }
}

      

Here is a live form (as pulled from the chrome debugger):

<form class="panel form-horizontal" method="post" action="/company/search" novalidate="novalidate">
            <div class="panel-heading">
                <h2 class="panel-title">
                    <i class="panel-title-icon fa fa-search fa-3x"></i>
                    Find a Company
                </h2>
            </div>
            <div class="panel-body padding-sm">
                <div class="row">
                    <div class="col-md-8 col-md-offset-2">
                        <label class="control-label sr-only" for="Category">Category</label>
                        <span role="status" aria-live="polite" class="ui-helper-hidden-accessible"></span>
                        <input type="text" id="Category" name="Category" value="Architecture" class="autocomplete required form-control form-group-margin ui-autocomplete-input" data-autocomplete-url="/api/servicecategory/search/" data-autocomplete-url-method="get" autocomplete="off">
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-8 col-md-offset-2">
                        <label class="control-label sr-only" for="CityState">City, State</label>
                        <span role="status" aria-live="polite" class="ui-helper-hidden-accessible"></span>
                        <input type="text" id="CityState" name="CityState" value="" class="autocomplete required form-control form-group-margin ui-autocomplete-input ui-autocomplete-loading" data-autocomplete-url="/api/companyservicelisting/area/cities/search" data-autocomplete-url-method="get" data-autocomplete-itemtemplate="CityAutocomplete" data-autocomplete-itemvalue="FormattedAddress" autocomplete="off">
                    </div>
                </div>
            </div>
            <div class="panel-footer text-center">
                <input type="submit" value="Search" id="Search" name="Search" class="btn btn-primary btn-lg">
            </div>
        </form>

      

And the controller code:

    [RoutePrefix("company/search")]
    public partial class CompanySearchController : Controller
    {
        [Route("")]
        public virtual ActionResult Search(String category, String cityState)
        {
            var model = ...; //This action works - search results view
            return View(MVC.CompanySearch.Views.Search, model);
        }

        [HttpPost]
        [Route("")]
        public virtual ActionResult SearchPost(CompanyListingParameters search)
        {   //parameter 'search' is null here
            return string.IsNullOrWhiteSpace(search.CityState) || string.IsNullOrWhiteSpace(search.Category)
                ? RedirectToAction(MVC.Home.Index())
                : RedirectToAction(MVC.CompanySearch.Search(search.Category.ToLower(), search.CityState.ToLower()));
        }
}

      

If I change the non-functional action to this, then it works (category and CityState are linked):

[HttpPost]
[Route("")]
public virtual ActionResult SearchPost(String Category, String CityState)
{   //parameters 'Category' and 'CityState' are populated on Post
    return string.IsNullOrWhiteSpace(CityState) || string.IsNullOrWhiteSpace(Category)
        ? RedirectToAction(MVC.Home.Index())
        : RedirectToAction(MVC.CompanySearch.Search(Category.ToLower(), CityState.ToLower()));
}

      

+3


source to share


1 answer


In your form remove name="Search"

from the submit button

<input type="submit" value="Search" id="Search" class="btn btn-primary btn-lg">

      



The value of the submit buttons is sent back, which is why you are submitting Search:"Search"

in the message, so ModelBinder

trying to bind typeof CompanyListingParameters

to a string (which of course doesn't work). And if you don't have javascript, the attribute id

seems like overkill.

+1


source







All Articles