【问题标题】:Double URL parameters on second controller method call第二个控制器方法调用上的双 URL 参数
【发布时间】:2015-11-16 03:00:08
【问题描述】:

我有一个控制器,它在我的索引视图中提交搜索字段时被调用。

  [System.Web.Mvc.HttpGet]
    public ActionResult ShowResultsWithFilter(SearchModelWithFilters searchModel)
    {
        //Logic and setting viewbag values

        return View(searchModel);
    }

网址返回我的结果。 作为

“url/Result/ShowResultsWithFilter?searchString=ASTRINGTOSEARCH”

我的提交模型

public class SearchModelWithFilters

    public string searchString { get; set; }
    public bool filterA { get; set; }
    public bool filterB { get;  set; }
    public bool filterC {get; set;}

    public SearchModelWithFilters()
    {
        //My initial values which should be defaut when first loading the results page
        filterA = true;
        filterB = true;
        filterC = true;
    }
}

搜索和过滤器也在结果页面上,因此我使用相同的控制器重新提交新的或相同的详细信息,并且 url 返回为

“url/Result/ShowResultsWithFilter?searchString=ASTRINGTOSEARCH&filterA=false&filterA=true&filterB=false&filterB=true&filterC=false&filterC=true”

每个过滤器的第一个实例始终为 false,第二个实例根据提交的值进行更新。

我希望它只显示正确更新的参数(我的过滤器),而不是加倍。

我的搜索字段视图(SearchField)

@model CodeCompareUk.Models.SearchModelWithFilters

<div class="row">
    <div class="col-md-12">
            <div class="input-group input-group-lg">
                @Html.TextBoxFor(m => m.searchString, new { @class = "form-control", @placeholder = "Enter Search..." })
                <a href="javascript:$('form').submit();" class="input-group-addon">
                    <i class="fa fa-search"></i>
                </a>
            </div>
    </div>
</div>

我把它放在这个表单助手里面,并且在结果页面中还包括部分“优化搜索”

@using (Html.BeginForm("ShowResultsWithFilter", "Result", method: FormMethod.Get))
   {

       @Html.Partial("SearchField")

       //also this is only added in results page
       @Html.Partial("RefineSearch")


}

我的优化搜索(RefineSearch)

@model CodeCompareUk.Models.SearchSubmitModelWithFilters
 @Html.CheckBoxFor(me => me.FilterA, new { @class = "custom-checkbox" })
  @Html.CheckBoxFor(me => me.FilterB, new { @class = "custom-checkbox" })
  @Html.CheckBoxFor(me => me.FilterC, new { @class = "custom-checkbox" })

谢谢

【问题讨论】:

  • 您能分享一下您的看法吗?
  • 已更新意见,谢谢

标签: c# asp.net-mvc


【解决方案1】:

这就是 CheckBoxFor 的工作原理。它创建 2 个&lt;inputs&gt; - 一个“隐藏”和第二个“复选框”。请注意,通常使用“POST”而不是“GET”提交表单。无论如何,请在此处查看有关 CheckBoxFor 的更多信息:

Why does the CheckBoxFor render an additional input tag, and how can I get the value using the FormCollection?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-02
    • 2012-04-14
    • 2017-12-10
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    • 2013-01-29
    相关资源
    最近更新 更多