mercredi 17 février 2016

ASP.NET MVC 4 Form Checkbox Group not sending all values to Controller

When I have Checkbox code in my form and it is submitted to the Controller, only the FIRST checkbox value is sent and all other checked values are ignored.

Example chunk of form:

@:<input type="checkbox" name="commPref" value="23" />Home Phone
@:<input type="checkbox" name="commPref" value="24" />Cell Phone
@:<input type="checkbox" name="commPref" value="Work" />Work Phone
@:<input type="checkbox" name="commPref" value="26" />Email
@:<input type="checkbox" name="commPref" value="27" />Mail

After submission to Controller:

  • if Cell Phone and Email are checked, "Model.commPref" = "24" -NOT- "24,26"
  • if Work Phone, Email, and Mail are checked, "Model.commPref" = "Work" -NOT- "Work,26,27"

I am completely baffled by this. Does there need to be extra code around the form checkboxes to keep the values grouped together? In ColdFusion, the form value that is sent back is a complete string of the values checked. The 'name' of the form controls are all the same (commPref), so this should be the resulting value for this field.

Edit: This is the Model description:

[StringLength(255)]
public string commPref { get; set; }




Aucun commentaire:

Enregistrer un commentaire