I have a form I am submitting and it has multiple checkboxes. The form is user defined so it has to be able to adapt to any size. I have a collection of "Addons" that I am using. The form displays the addons in checkboxes, with each addon having 4 options. They can select one option on each addon (jQuery to limit one checkox selected) but none are required.
The problem I have is when they submit the form if the first checkbox does not have a checked option than the rest are not passed since it comes in a list/collection How can I make these checkboxes optional and pass the data to my controller?
<h1>Pick Your Extras To Make It More Special</h1>
<fieldset>
<ul>
<li>Extra <br /><img src="UPS_30484.jpg" /></li>
<li><label>One <br /> $1.25<br /> <input type="checkbox" name="Addons[0]" value="1" class="addon-checkboxes"></label></li>
<li><label>Two <br /> $2.50<br /> <input type="checkbox" name="Addons[0]" value="2" class="addon-checkboxes"></label></li>
<li><label>Tree <br /> $3.00<br /> <input type="checkbox" name="Addons[0]" value="3" class="addon-checkboxes"></label></li>
<li><label>Four <br /> $4.00<br /> <input type="checkbox" name="Addons[0]" value="4" class="addon-checkboxes"></label></li>
</ul>
</fieldset>
<fieldset>
<ul>
<li>Extra 2 <br /><img src="UPS_30484.jpg" /></li>
<li><label>One <br /> $2.50<br /> <input type="checkbox" name="Addons[1]" value="5" class="addon-checkboxes"></label></li>
<li><label>Two <br /> $5.00<br /> <input type="checkbox" name="Addons[1]" value="6" class="addon-checkboxes"></label></li>
<li><label>Three <br /> $7.50<br /> <input type="checkbox" name="Addons[1]" value="7" class="addon-checkboxes"></label></li>
<li><label>Four <br /> $10.00<br /> <input type="checkbox" name="Addons[1]" value="8" class="addon-checkboxes"></label></li>
</ul>
</fieldset>
<fieldset>
<ul>
<li>Candy <br /><img src="30484.jpg" /></li>
<li><label>Small<br /> $5.00<br /> <input type="checkbox" name="Addons[2]" value="13" class="addon-checkboxes"></label></li>
<li><label>Medium<br /> $10.00<br /> <input type="checkbox" name="Addons[2]" value="14" class="addon-checkboxes"></label></li>
<li><label>Large<br /> $15.00<br /> <input type="checkbox" name="Addons[2]" value="15" class="addon-checkboxes"></label></li>
<li><label>Bucket<br /> $20.00<br /> <input type="checkbox" name="Addons[2]" value="16" class="addon-checkboxes"></label></li>
</ul>
</fieldset>
The values on each one represent a price in the database that has a foreign key to the addon product. So I get a list of Addons but when Addons[0]
does not have something selected, the Addons[1]
and Addons[2]
are not submitted either. If all are selected the values do go through to the controller, but I need these to be optional.
Model for the view when submitted:
public class ProductToCartViewModel : BaseObject
{
[Required]
public int Price { get; set; }
public int[] Addons { get; set; }
public string DeliveryZipCode { get; set; }
public DateTime DeliveryDate { get; set; }
public ApplicationUser User { get; set; }
}