我的FormMethod.Get请求发送多个值如何在ASP.net MVC中修复此问题?

时间:2013-11-19 15:09:13

标签: asp.net-mvc razor

我有一个mvc5网页,它有一个表单方法,当选中复选框时,提交表单并发送get请求。请求标头按预期提交CarNumber和CarId,但是标题中的复选框值被发送两次,为什么会发生这种情况?我该如何解决这个问题。

标题值像这样重复

ShowAllDesignReviews=true&ShowAllDesignReviews=false


@using (Html.BeginForm("Index", "DesignReview", FormMethod.Get))
 {

@Html.Hidden("CarNumber", "CarNumber")
@Html.Hidden("CarId", "CarId")
@Html.CheckBoxFor(model => Model.ShowAllDesignReviews, new { @onchange = "this.form.submit()" })

}

2 个答案:

答案 0 :(得分:1)

这就是MVC中CheckBoxFor帮助器生成html的方式,以便于模型绑定

<input name="ShowAllDesignReviews" type="checkbox" value="true" />
<input name="ShowAllDesignReviews" type="hidden" value="false" /> 

If you dont select check box, the field will not be posted.要使值(false)传递,他们使用Hidden。

参考asp.net mvc: why is Html.CheckBox generating an additional hidden input

最好让您的控制器接受模型

public ActionResult Save(Design model)
{
var boolSet=model.ShowAllDesignReviews;
}

答案 1 :(得分:0)

请参阅Murali关于为什么会发生这种情况的答案。 我有一些类似的场景,我想要一个“标准”复选框,所以我写了下面的HtmlHelper:

public static class StandardCheckboxForHelper
{
    public static MvcHtmlString StandardCheckboxFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> propertyExpression)
    {
        return html.StandardCheckboxFor(propertyExpression, null);
    }

    public static MvcHtmlString StandardCheckboxFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> propertyExpression, object htmlAttributes)
    {
        return html.StandardCheckboxFor(propertyExpression, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
    }


    public static MvcHtmlString StandardCheckboxFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> propertyExpression, IDictionary<string, object> htmlAttributes)
    {
        var propertyName = html.NameFor(propertyExpression).ToString();
        var propertyId = html.IdFor(propertyExpression).ToString();
        var propertyValueString = html.ValueFor(propertyExpression).ToString();
        var propertyValue = ModelMetadata.FromLambdaExpression(propertyExpression, html.ViewData).Model;
        var unobtrusiveValidationAttributes = html.GetUnobtrusiveValidationAttributes(propertyName, ModelMetadata.FromLambdaExpression(propertyExpression, html.ViewData));

        var checkbox = new TagBuilder("input");
        checkbox.MergeAttribute("type", "checkbox");
        checkbox.MergeAttribute("id", propertyId);
        checkbox.MergeAttribute("name", propertyName);
        checkbox.MergeAttributes(unobtrusiveValidationAttributes);
        checkbox.MergeAttributes(htmlAttributes);
        bool propertyValueConverted;
        if ((propertyValue is bool) && (bool) propertyValue)
            checkbox.MergeAttribute("checked", "checked");
        else if ((propertyValue is bool?) && ((bool?)propertyValue).GetValueOrDefault(false))
            checkbox.MergeAttribute("checked", "checked");
        else if (bool.TryParse(propertyValueString, out propertyValueConverted) && propertyValueConverted)
            checkbox.MergeAttribute("checked", "checked");
        return checkbox.ToHtml();
    }
}

用法与CheckboxFor帮助器相同,它只会呈现一个简单的复选框。