check box before item using viewBag

Im trying to read from database into the form of a list with check box each

I have in my controller:

 var data2 = from p in db.Teams select p.teamName;
            SelectList list2 = new SelectList(data2);
            ViewBag.Teams = list2;

and in my view:

 <div class="form-group">
        <label class="col-md-2 control-label">
            Select Team
        </label>
        <div class="col-md-10">
            @foreach (var T in (SelectList)ViewBag.Teams)
            {
                <input type="checkbox" name="SelectedTeams" value="@T.Value" class="checkbox-inline" />
                @Html.Label(T.Value, new { @class = "control-label" })
            }
        </div>
    </div>

the problem is that when I debug it returns an error that T.value cann’t be null although Im sure that viewBag.Teams is returning data

what is wrong?

Is it possible that you have any null entries within your Teams collection? I suppose you could use a bit of LINQ and explicitly only grab teams with names that were available :

// Get your non-null teams
var data2 = db.Teams.Where(t => t.teamName != null)
                    .Select(t => t.teamName)
                    .ToList();
// Store the teams in the ViewBag (as a SelectList)
ViewBag.Teams = new SelectList(list2);

Is there any reason you are using a SelectList for this? You would really only need to do that if you were binding to a DropDownList. If that isn’t the case, then simply just store your Teams in the ViewBag as a collection :

ViewBag.Teams = list2;

and then just iterate through those :

@foreach (var team in ViewBag.Teams)
{
                <input type="checkbox" name="SelectedTeams" value="@team" class="checkbox-inline" />
                @Html.Label(team, new { @class = "control-label" })
}

sorry but have you really read my question up there? I clearly said Im sure that the viewBag is returning data (Teams) so there is no need for the LINQ u suggested and you just recopied the HTML code  I already have.

so this is not an answer sorry

I think Rion meant this

// Get your non-null teams
var data2 = db.Teams.Where(t => t.teamName != null)
                    .Select(t => t.teamName)
                    .ToList();

ViewBag.Teams = data2;

You don’t know for sure you have no null values, you simply think you don’t.  99% of bugs in code are when people make assumptions about things and when you get unexpected behavior you need to learn to put your assumptions aside and do some actual debugging
to see what the reality is, because sitting there thinking "I don’t think I have any null values so I’m not going to even check or change my code" isn’t going to get you anywhere.

AidyF

because sitting there thinking "I don’t think I have any null values so I’m not going to even check or change my code

I didn’t say " I think I don’t have null values" I said Im sure I dont have null values because I tested it and displayed the ViewBag.Teamsin a drop down list as well and data was retrieved ;)

So what is the problem then?

the problem is that when I debug it returns an error that T.value cann’t be null although Im sure that viewBag.Teams is returning data

lolo512

sorry but have you really read my question up there? I clearly said Im sure that the viewBag is returning data (Teams) so there is no need for the LINQ u suggested and you just recopied the HTML code  I already have.

so this is not an answer sorry

Firstly, thanks for that.

It appears I had a typo within my initial post and Aidy seemed to have recognized where it was and my intention. I was recommending that you actually store the collection itself within the ViewBag instead of using a SelectList to see if you experienced any
different results :

// Get your non-null teams
var data2 = db.Teams.Where(t => t.teamName != null)
                    .Select(t => t.teamName)
                    .ToList();

// Store the actual collection in the ViewBag (not using a SelectList)
ViewBag.Teams = data2;

And then if you needed to populate your values within View, you could just iterate through your values as seen below :

@foreach (var team in ViewBag.Teams)
{
                <input type="checkbox" name="SelectedTeams" value="@team" class="checkbox-inline" />
                @Html.Label(team, new { @class = "control-label" })
}

or if you needed to store these same values in a DropDownList, you could cast them as a SelectList :

@Html.DropDownListFor(m => m.YourProperty, new SelectList(ViewBag.Teams))

There are a variety of things that could be going wrong here that proper debugging could easily resolve. You could consider storing your values in a code-block at the top of your View to ensure that your ViewBag items were cast properly :

@{
    // Place a breakpoint here to take a look at ViewBag.Teams and see what teams looks like after the cast
    var teams = (SelectList)ViewBag.Teams;
}

Without having access to your specific database, details of your entities and what your entire View looks like, we are simply providing the best responses that are available.

Leave a Reply