gpt4 book ai didi

c# - 为什么我的 MVC5 Post Action 模型没有被填充

转载 作者:行者123 更新时间:2023-11-30 17:45:37 28 4
gpt4 key购买 nike

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

这是不会在我的 Post 操作中绑定(bind)的模型:

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>

Controller 代码:

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

如果我将非功能性操作更改为此,那么它将起作用(绑定(bind)了 Category 和 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()));
}

最佳答案

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

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

提交按钮的值被回发,所以你在帖子中发送 Search:"Search",所以 ModelBinder 试图绑定(bind) typeof CompanyListingParameters 到一个字符串(当然会失败)。除非你有一些 javascript,否则 id 属性似乎是不必要的。

关于c# - 为什么我的 MVC5 Post Action 模型没有被填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27282412/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com