C# 在MVC3中使用Get方法提交表单?

C# 在MVC3中使用Get方法提交表单?,c#,asp.net,asp.net-mvc,asp.net-mvc-3,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 3,我有以下表格: @model Teesa.Models.SearchModel @using (Html.BeginForm("Index", "Search", FormMethod.Get, new { id = "SearchForm" })) { <div class="top-menu-search-buttons-div"> @if (!MvcHtmlString.IsNullOrEmpty(Html.ValidationMessageFor(m

我有以下表格:

@model Teesa.Models.SearchModel
@using (Html.BeginForm("Index", "Search", FormMethod.Get, new { id = "SearchForm" }))
{
    <div class="top-menu-search-buttons-div">
        @if (!MvcHtmlString.IsNullOrEmpty(Html.ValidationMessageFor(m => m.SearchText)))
        {
            <style type="text/css">
                .top-menu-search-text
                {
                    border: solid 1px red;
                }
            </style>
        }
        @Html.TextBoxFor(q => q.SearchText, new { @class = "top-menu-search-text", id = "SearchText", name = "SearchText" })
        @Html.HiddenFor(q=>q.Page)
        <input type="submit" value="search" class="top-menu-search-submit-button" />
    </div>
    <div id="top-menu-search-info" class="top-menu-search-info-div">
        Please Select one :
        <hr style="background-color: #ccc; height: 1px;" />
        <div class="top-menu-search-info-checkbox-div">
            @Html.CheckBoxFor(q => q.SearchInBooks, new { id = "SearchInBooks", name = "SearchInBooks" })
            <label for="SearchInBooks">Books</label>
        </div>
        <div class="top-menu-search-info-checkbox-div">
            @Html.CheckBoxFor(q => q.SearchInAuthors, new { id = "SearchInAuthors" })
            <label for="SearchInAuthors">Authors</label>
        </div>
        <div class="top-menu-search-info-checkbox-div">
            @Html.CheckBoxFor(q => q.SearchInTags, new { id = "SearchInTags" })
            <label for="SearchInTags">Tags</label>
        </div>
}
但必须是这样的:

http://localhost:2817/Search?SearchText=book&Page=2&SearchInBooks=true&SearchInAuthors=true&SearchInTags=true
我怎样才能修好它?

谢谢

Html。Checkbox
(以及相关的
For…
方法)为false生成隐藏输入,为true生成隐藏输入。这是为了确保绑定时模型绑定工作一致

如果必须清除隐藏输入产生的“false”项,则需要自己构造复选框输入(使用HTML而不是帮助器)


为什么不创建一个名与Get方法匹配的Post方法呢。这将确保代码更易于调试。因为你将不会有一个巨大的功能去经历试图找到这样的问题

我找不到一个你从哪里得到重复的url查询字符串


这也将允许您将结果绑定回模型。

如果您希望模型绑定成功,则必须采用这种方式,因为这是
Html.CheckBox/Html.CheckBoxFor
方法的本质,它们也将呈现隐藏字段

我建议你还是去邮局吧,这样你的生活就轻松多了。如果您仍然想使用GET,那么您必须直接使用checkbox元素,但必须处理模型绑定问题。当复选框被选中时,并非所有浏览器都返回“true”。firefox会传递“on”,因此默认的模型绑定器会抛出一个错误


其他可选选项是,您可以使用自定义模型绑定器,也可以通过侦听提交事件使用jquery提交表单。

那么,如何将表单绑定到模型!当您执行post方法时,它会将结果绑定回模型。看看我的答案@Mohammad在模型绑定方面,您不必做任何更改。这一部分应该仍然可以在示例中使用(模型绑定也可以在查询字符串参数中使用)。我猜当您使用普通复选框时,从firefox提交表单时,您将在模型绑定中遇到麻烦。当选中复选框时,Firefox将以“on”的形式发送值,您将获得一个绑定error@Mohammad您可以覆盖
SearchModel
的绑定,也可以使用jQuery拦截表单提交,并构造重定向URL,使其安全地使用'true'/'false'。在这个实例中使用GET的全部目的是确保用户可以将结果页添加到书签中,而查询是完整的,这在使用POST时是不可能的。@Zhaph BenDuguid啊,我明白了。我不知道这一点,我自己也是MVC新手。谢谢你的提示。
http://localhost:2817/Search?SearchText=book&Page=2&SearchInBooks=true&SearchInBooks=false&SearchInAuthors=true&SearchInAuthors=false&SearchInTags=true&SearchInTags=false
http://localhost:2817/Search?SearchText=book&Page=2&SearchInBooks=true&SearchInAuthors=true&SearchInTags=true
<input type="checkbox" id="SearchInBooks" name="SearchInBooks">