14

在 ASP.NET MVC RC 中的 Html.Checkbox 中提交值的疯狂问题

一些值只是没有来到 Request.Params

在我的表格中,我在循环中有这条线:

<%=Html.CheckBox("cb" + p.Option.Id, p.Option.IsAllowed, new { value = 6 })%>

它呈现到下一个:

    <input checked="checked" id="cb17" name="cb17" type="checkbox" value="6" />
<input name="cb17" type="hidden" value="false" /> 

    <input checked="checked" id="cb18" name="cb18" type="checkbox" value="6" />
<input name="cb18" type="hidden" value="false" /> 

    <input id="cb19" name="cb19" type="checkbox" value="6" />
<input name="cb19" type="hidden" value="false" />

    <input id="cb20" name="cb20" type="checkbox" value="6" />
<input name="cb20" type="hidden" value="false" />

    <input checked="checked" id="cb21" name="cb21" type="checkbox" value="6" />
<input name="cb21" type="hidden" value="false" /> 

提交表单后,我得到如下信息:

Form.Params["cb17"] = {6, "false"}
Form.Params["cb18"] = {6, "false"}
Form.Params["cb19"] = {"false"}
Form.Params["cb20"] = {"6,false"}
Form.Params["cb21"] = {"false"}

在请求字符串中有些参数显示两次(正常情况),有些只显示一次(只有隐藏字段的值)。似乎它不依赖于复选框是否被选中,值是否发生了变化......

有人遇到过这种情况吗?我该如何解决?

4

4 回答 4

16
   <% using(Html.BeginForm("Retrieve", "Home")) %>//Retrieve is the name of the action while Home is the name of the controller
       <% { %>
    <%foreach (var app in newApps)              { %>  
  <tr> 
       <td><%=Html.CheckBox(""+app.ApplicationId )%></td>      

   </tr>  
<%} %>
 <input type"submit"/>
<% } %>

在你的控制器中

 List<app>=newApps; //Database bind
 for(int i=0; i<app.Count;i++)
 {

    var checkbox=Request.Form[""+app[i].ApplicationId];
    if(checkbox!="false")// if not false then true,false is returned
 }

您检查 false 的原因是因为 Html Checkbox 助手为 value true 做了一些奇怪的事情

真返回为:

it makes the string read "true, false"

所以你可能认为这是两个值,但它只是一个值,并且意味着真实

False 返回为:

it makes the string read "false"
于 2009-03-18T14:33:18.203 回答
10

这实际上是它应该按照规范工作的方式。

它与 ASP.NET MVC 无关,但是当未选中复选框时,它包含在 POST 集合中。

你得到两个值,因为你有一个复选框和一个同名的输入(你有两个值的很可能是复选框选中的那个)。

编辑:来自 W3C 的规范

于 2009-03-18T14:26:44.303 回答
2

在表单提交之后/保存之前(无状态模式)无需向数据库询问 id,我已经生成了这样的代码:

    foreach (string key in Request.Form)
    {
        var checkbox = String.Empty;
        if (key.StartsWith("cb"))
        {
          checkbox = Request.Form["" + key];

          if (checkbox != "false")
          {
              int id = Convert.ToInt32(key.Remove(0, 2));
          }
        }
    }

谢谢你们帮助我解决这个问题!

于 2009-03-18T15:11:08.643 回答
0

我用这个:

public struct EditedCheckboxValue
{
    public bool Current { get; private set; }

    public bool Previous { get; private set; }

    public bool Changed { get; private set; }

    public EditedCheckboxValue(System.Web.Mvc.FormCollection collection, string checkboxID) : this()
    {
        string[] values = collection[checkboxID].Split(new char[] { ',' });
        if (values.Length == 2)
        {   // checkbox value changed, Format: current,old
            Current = bool.Parse(values[0]);
            Previous = bool.Parse(values[1]);
            Changed = (Current != Previous);
        }
        else if (values.Length == 1)
        {
            Current = bool.Parse(values[0]);
            Previous = Current;
            Changed = false;
        }
        else
            throw new FormatException("invalid format for edited checkbox value in FormCollection");
    }
}

然后这样称呼它:

EditedCheckboxValue issomething = new EditedCheckboxValue(collection, "FieldName");
instance.IsSomething = issomething.Current;
于 2012-09-26T17:31:35.803 回答