question

mohdmazharkhan-1078 avatar image
0 Votes"
mohdmazharkhan-1078 asked YijingSun-MSFT answered

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

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>

                 }
dotnet-csharpdotnet-aspnet-mvc
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

See if the following may be of assistance.


1 Vote 1 ·
YijingSun-MSFT avatar image
0 Votes"
YijingSun-MSFT answered

Hi @mohdmazharkhan-1078 ,
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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered Bruce-SqlWork edited

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) 
      } 
 } 
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.