I need to figure out a way to create a List or SelectList for checkboxes for multiple properties in my database.
The Model has UrgentNeedsFood, UrgentNeedsMedication, UrgentNeedsOther as individual properties and I need to create checkboxes for each of those properties to display. So if the user clicks one or all of the checkboxes the value of that respective checkbox gets saved to each individual property in the database.
I would like to use a List or SelectList to do the model binding and to get/set the value from/to the database. Without a ViewModel to represent the properties, how do I go about doing this?
Here is the Model:
This is what I'm trying to do in my Controller:
SelectListItem list = new SelectListItem();
list = new SelectListItem { Text = "Medication", Value = "True", Selected = false };
list = new SelectListItem { Text = "Food", Value = "True", Selected = false };
list = new SelectListItem { Text = "Life/Health/Safety", Value = "True", Selected = false };
list = new SelectListItem { Text = "Other", Value = "True", Selected = false };
ViewBag.UrgentNeeds = list;
When trying the above code I use this in my View:
@foreach (SelectListItem item in ViewBag.UrgentNeeds)
{
if (item.Text == "Medication"){<input type="checkbox" name="Urgent_Needs_Medication" /> }
if (item.Text == "Food"){ <input type="checkbox" name="Urgent_Needs_Food" /> }
if (item.Text == "Life/Health/Safety"){ <input type="checkbox" name="Urgent_Needs_Life_Health_Safety" /> }
if (item.Text == "Other"){ <input type="checkbox" name="Urgent_Needs_Other" /> }
break;
}
But I get this error:
Cannot implicitly convert type 'System.Web.Mvc.SelectListItem' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?)
When I'm not trying the above code, I get this View returned from the database:
Here is the code for the View:
<div class="col-md-9">
<div class="row">
<div class="col-md-4">
<input type="checkbox" name="Urgent_Needs_Medication" value="true" /> @Html.LabelFor(model => model.Urgent_Needs_Medication, new { @class = "control-label" })
</div>
<div class="col-md-4">
<input type="checkbox" name="Urgent_Needs_Life_Health_Safety" value="true" /> @Html.LabelFor(model => model.Urgent_Needs_Life_Health_Safety, new { @class = "control-label" })
</div>
</div>
<div class="row">
<div class="col-md-4">
<input type="checkbox" name="Urgent_Needs_Food" value="true" /> @Html.LabelFor(model => model.Urgent_Needs_Food, new { @class = "control-label" })
</div>
<div class="col-md-4">
<input type="checkbox" name="Urgent_Needs_Other" value="true" /> @Html.LabelFor(model => model.Urgent_Needs_Other, new { @class = "control-label" })
</div>
</div>
</div>
As you can see none of the checkboxes are checked and I know they should be, but I don't know how to get the checked property to checked by the controller.
Does anyone have some insight on how to get the value from the database and assign the checked value in the controller?
This is simple enough as a dropdownlist, but I need to use checkboxes instead.
-- UPDATE --
I see the datatype in the database is bit
. EF has the datatype as Nullable<bool>
. They do talk to one another, but I'm not sure if that would make a difference.
Instead of using for each checkbox, I used the following for each checkbox:
This at least returned the correct results from the database. This also changed the data in the database when I checked or unchecked the checkbox.