0

I'm currently working on an MVC application and save some information into a database. From this information I save some checkbox values which is saved in its own column as

string 1, string 2, string 3

Now I'm trying to retrieve these values and make the checkbox checked in a different page. However where I should be getting two checkboxes I only get one. The returned values are correct but for some strange reason only one checkbox is displayed in the view

In my controller I have the following code

IEnumerable<MyEntity> myEntity = entityRepo.GetAll().Where(a => a.UserId == id);
List<CheckBox> checkBoxList = new List<CheckBox>();
foreach (var items in myEntity)
{
     checkBoxList.Add(
     new CheckBox
     {
         Text = items.EightWaste,
         Checked = true,
         Value = items.EightWaste,
     });
 }

Then in the view I've got

@foreach(var items in Model.EightWatseChkBox)
{
   @Html.DisplayFor(model => items.Text)
   @Html.CheckBoxFor(model => items.Checked)
}

And my UI output looks like

Output

Can someone tell me where I'm going wrong please.

Izzy
  • 6,740
  • 7
  • 40
  • 84
  • What is the value of `myEntity.Count();`? - it look like you only have one item and its `EightWaste` value is `"Defects, Transportation"` –  May 10 '16 at 11:22
  • And as a side note, you `foreach` loop will not bind to you model. You need to use a `for` loop or `EditorTemplate` for typeof `CheckBox` –  May 10 '16 at 11:23
  • Have you checked this question : http://stackoverflow.com/questions/14730746/getting-checkbox-value-in-asp-net-mvc-4 ? – JJP May 10 '16 at 11:23
  • All checkboxes in the list saved to one field in the table? So you only get one record in myEntity. – Terry Delahunt May 10 '16 at 11:24
  • @StephenMuecke The count is 1 – Izzy May 10 '16 at 11:27
  • @TerryDelahunt Yes I have only one record in myEntity – Izzy May 10 '16 at 11:27
  • 2
    @Code, Then I assume you want to create an array using `String.Split()` (and then `String.Join()` to join the selected items back into a single string in the POST method) –  May 10 '16 at 11:31
  • @StephenMuecke Can you provide an example please – Izzy May 10 '16 at 11:33
  • Sure, give me 20 min. –  May 10 '16 at 11:33
  • @StephenMuecke Thanks – Izzy May 10 '16 at 11:34
  • @StephenMuecke This is how I currently save them into the database `string.Join(",", Request.Form["eigthwaste"].Split(',').Where(a => !a.Contains("false")));` – Izzy May 10 '16 at 11:36
  • as Stephen stated, you need separate the string returned from the db into an array or some form of list and iterate thru that to add each individual checkbox. – Terry Delahunt May 10 '16 at 12:46

2 Answers2

1

Your values are stored in on field as a comma separated string, so to generate a checkbox for each 'word', you need to split the string to an array.

MyEntity data = entityRepo.GetAll().Where(a => a.UserId == id).FirstOrDefault();

Model.EightWatseChkBox = data.EightWaste.Split(new char[]{ ',' }).Select(x => new CheckBox()
{
    Text = x,
    Checked = true
}).ToList();

Note that property EightWatseChkBox in your model needs to be IList<CheckBox> not IEnumerable<CheckBox> so that you can use a for loop in the view

@for (int i = 0; i < Model.EightWatseChkBox.Count; i++)
{
    @Html.DisplayFor(m => m.EightWatseChkBox[i].Text)
    @Html.CheckBoxFor(m => m.EightWatseChkBox[i].Checked)
}

and in the POST method, you can join the selected values back again

string[] selected = model.EightWatseChkBox.Where(x => x.Checked).Select(x => x.Text);
string combined = String.Join(",", selected);

For more information on why you cannot use a foreach loop to bind to a collection, refer this answer (note if you cannot change the model property to IList<CheckBox>, then you can use the EditorTemplate option)

Community
  • 1
  • 1
  • The property in my model is of `List`. Thanks for this, I'll have a go with this and let you know how I get on – Izzy May 10 '16 at 11:55
-1

Try this,

In your Controller write like below;

IEnumerable<MyEntity> myEntity = entityRepo.GetAll().Where(a => a.UserId == id);

ViewBag.Chekboxlist = myEntity;

And then your view will be

@foreach(var items in Model.EightWatseChkBox)
{
       @Html.DisplayFor(model => items.Text)
       if(ViewBag.Chekboxlist != null)
       {
           IEnumerable<MyEntity> myEntity = (IEnumerable<MyEntity>)ViewBag.Chekboxlist;
           foreach(var item in myEntity )
           {
               @Html.CheckBoxFor(model => item.Checked)
           }
      }   
}
Kinjal Gohil
  • 958
  • 9
  • 15