[RESOLVED]Problem with CheckBoxListFor in MVC

Hello Everyone, I am trying to use checkboxlistfor in mvc to generate checkboxes dynamically for my items.Below is the my View code for checkboxlistfor:

<table class="form-table">
<tr>
<td>
<label>Searchfor</label>
</td>
<td>
@Html.CheckBoxListFor(model => model.PostedSearchableObject.PostedIds,
model => model.AvailableObjectTypes,
objecttype => objecttype.Id,
objecttype => objecttype.Name,
model => model.SelectedObjectTypes,
Position.Horizontal) </td>

When I run the solution I can see all the items that I need to have a checkbox for (ex: item1, item2, item3), but the problem is there are no Check-boxes next to each item for the user to select them. they are just a simple labels without a check box.

Does anyone has any idea why I can’t see the check-boxes?

thanks. 

Hi Benjaminsh,

Thanks for your post.

Benjaminsh

but the problem is there are no Check-boxes next to each item for the user to select them. they are just a simple labels without a check box.

According to your requirement,I did a sample.please refer to the following sample:

controller:

 [HttpGet]
        public ActionResult demo1()
        {
            WebCSDBEntities db = new WebCSDBEntities();
            return View(db.tblCities);
        }
        [HttpPost]
        public string demo1(IEnumerable<tblCity> cities)
        {
            if (cities.Count(x => x.IsSelected_) == 0)
            {
                return "you have not selected any city";
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("You selected-");
                foreach (tblCity city in cities)
                {
                    if (city.IsSelected_)
                    {
                        sb.Append(city.Name_ + ",");
                    }
                }
                sb.Remove(sb.ToString().LastIndexOf(","),1);
                return sb.ToString();
            }
        }

view:

@model reflectDemo.Models.tblCity

@{
    ViewBag.Title = "tblCity";
}

@*<h2>tblCity</h2>*@
@Html.HiddenFor(x=>x.Id)
@Html.HiddenFor(x=>x.Name_)
@Html.CheckBoxFor(x=>x.IsSelected_)
@Html.DisplayFor(x=>x.Name_)
@model IEnumerable<reflectDemo.Models.tblCity>

@{
    ViewBag.Title = "demo1";
}



<div style="font-family:Arial">
    <h2>demo1</h2>
    @using (Html.BeginForm())
    {
        @Html.EditorForModel()
        <br />
        <input type="submit" value="submit" /> 
    }
    
</div>

It worked on my side.

More information,you can check this:

#ASP.Net MVC- How to implement CheckBoxList

http://besttechnologyblog.com/2014/05/04/asp-net-mvc-how-to-implement-checkboxlist/

Hope this can be helpful.

Best Regards,

Eileen

Thank you very much for the time you spend to reply this thread. I found out the problem was due to overriding a CSS class.

Leave a Reply