Last post May 21, 2018 01:59 PM by bruce (sqlwork.com)
May 21, 2018 01:51 PM|MedNasser|LINK
I have two tables: User and Interest.
The user must select his interests upon subscription. The interests are displayed as check-boxes. I have managed to display the list of the interests in the database as checkboxes in the sign up view but when it comes to retrieving the values from the checkboxes
i get an exception saying "Object reference not set to an instance of an object".
This is the methods:
public ActionResult SignUp()
var data2 = (from i in db.Interest
// Store the actual collection in the ViewBag (not using a SelectList)
ViewBag.Interests = data2;
UserSignUpView TSV = new UserSignUpView();
public ActionResult SignUp(UserSignUpView TSV, HttpPostedFileBase im, HttpPostedFileBase cv, string SelectedInterests)
This is the view:
@foreach (var interest in ViewBag.Interests)
<input type="checkbox" name="SelectedInterests" value="@interest.InterestId" class="checkbox-inline" />
While debugging, the result of the checkboxes reaches the method's entry in the controller (string SelectedInterests) but the @foreach (var interest in ViewBag.Interests) part shows that exception.
May 21, 2018 01:59 PM|bruce (sqlwork.com)|LINK
the postback controller method needs to reload the viewbag. the view bag is only passed from the controller to the view.