View-bag to fill checkbox-list using asp.net mvc

coder rock 436 Reputation points
2021-07-02T07:55:21.787+00:00

Below is my model class

   public class InformationMissing : AbstractDBObject
  {
     public int Id { get; set; }
     public string PatientDemographics { get; set; }
     public string InsuranceDetails { get; set; }
     public string PhysicianDetails { get; set; }
     public string Note { get; set; }
     public int personId {get;set;}
     public string status { get; set; }
     public List<CheckboxList_model> RetrievalItem { get; set; }
     }
    public class CheckboxList_model
    {
      public string Text { get; set; }
       public int Value { get; set; }
      public Boolean Selected { get; set; }
     }

below is my controller method

     public ActionResult Process()
    {
   List<CheckboxList_model> chkRetrieval = new List<CheckboxList_model>(){
      new CheckboxList_model{ Text="One", Value = 1 ,Selected=false},
      new CheckboxList_model{ Text="Two", Value = 2 ,Selected=false},
      new CheckboxList_model{ Text="Three", Value = 3 ,Selected=false }
      };

      ViewBag.Retrieval = chkRetrieval;
     return View();
    }

Below code i have tried but checkboxlist is not filling

   @foreach (var Retrieval in ViewBag.Retrieval)
       {

@Html.CheckBoxFor(Retrieval.Selected, new { @class = "flat" })

@Html.DisplayFor(Retrieval.Text)

@Html.HiddenFor(Retrieval.Value)

       }

From below foraech able to see text value but above checkboxlist is not filling

@foreach (var Retrieval in ViewBag.Retrieval)
        {
             <p>
@Retrieval.Text </p>

                }
Developer technologies ASP.NET Other
Developer technologies C#
{count} votes

Accepted answer
  1. Yijing Sun-MSFT 7,096 Reputation points
    2021-07-05T05:25:15.877+00:00

    Hi @coder rock ,
    I think you could use CheckBoxList.Just like this:

    @Html.CheckBoxList("chkModels", (ViewBag.Retrieval as List<SelectListItem>))  
    

    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2021-07-04T01:00:48.47+00:00

    The html helpers use reflection to find the property to display. (The for helpers convert the lambda expression to a text string). While reflection can find ViewBag.Retrieval, it’s an list and does not have the properties.

    You really should not be binding to the viewbag, but if you use array syntax it should work.

        @{ 
         var Retrieval = (List<CheckboxList_model) ViewBag.Retrieval; 
         for (var I =0; I< Retrieval.Count; ++i ) 
         { 
                @Html.CheckBoxFor(Retrieval[i]Selected, new { @class = "flat" }) 
                @Html.DisplayFor(Retrieval[i].Text) 
                @Html.HiddenFor(Retrieval[i].Value) 
         } 
    } 
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.