为什么我的MVC5 Post Action模型没有填充

时间:2014-12-03 21:54:20

标签: c# asp.net-mvc asp.net-mvc-5

我的解决方案中有一个模型没有填充在我的POST表单操作中。为什么这个模型不同?

以下是我的Post动作中不会绑定的模型:

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

这是实时表单(从chrome调试器中提取):

<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>

控制器代码:

    [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()));
        }
}

如果我将非功能性操作更改为此操作,则它可以正常工作(类别和CityState已绑定):

[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()));
}

1 个答案:

答案 0 :(得分:1)

在表单中,从提交按钮中删除name="Search"

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

提交按钮的值会被回发,因此您要在帖子中发送Search:"Search",因此ModelBinder正在尝试将typeof CompanyListingParameters绑定到字符串(当然这会失败) )。除非你有一些javascript,否则id属性似乎没必要。