MVC Html.CheckBox and form submit issue

后端 未结 4 1556
醉酒成梦
醉酒成梦 2020-12-17 16:11

Crazy issue with submitting of values in Html.Checkbox in ASP.NET MVC RC

Some of the values are just not come to Request.Params

At my form I have this line i

相关标签:
4条回答
  • 2020-12-17 16:39

    Without the need to ask database about the ids after form submitting/before saving (Stateless mode) I've produced such code:

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

    Thanks you guys to help me work around this issue!

    0 讨论(0)
  • 2020-12-17 16:40

    This is actually the way it should work according to specifications.

    It has nothing to do with ASP.NET MVC, but when a check box is left unchecked it is not included in the POST collection.

    You get two values because you have both a checkbox and an input with the same name (and the ones you have two values for are most likely the ones with checkboxes checked).

    Edit: specifications from W3C

    0 讨论(0)
  • 2020-12-17 16:46
       <% 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"/>
    <% } %>
    

    and in your controller

     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
     }
    

    the reason you check for false because the Html Checkbox helper does some kind of freaky thing for value true

    True returns as:

    it makes the string read "true, false"
    

    so you may have thought it was two values but its just one and means true

    False returns as:

    it makes the string read "false"
    
    0 讨论(0)
  • 2020-12-17 16:55

    I use this:

    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");
        }
    }
    

    and then call it like this:

    EditedCheckboxValue issomething = new EditedCheckboxValue(collection, "FieldName");
    instance.IsSomething = issomething.Current;
    
    0 讨论(0)
提交回复
热议问题