1

I would like to have a checkbox for my Online_Ballot, where a checkbox contains of candidates where a voter could vote for a specific candidates.

Below is my code.

CandidatesViewModel.cs

public class CandidatesViewModel
{
    public IEnumerable<candidates> AvailableCandidates { get;set; }
    public IEnumerable<Candidates> SelectedCandidates { get; set; }
    public PostedCandidates PostedCandidates { get; set; }
}

public class PostedCandidates
{
    public string[] CandidatesId { get; set; }
}

Candidates.cs

public class Candidates
{
    public int candidates_info_id { get; set; }
    public string candidates_fullname { get; set; }
    public object Tags { get; set; }  
    public bool IsSelected { get; set; }
}

Controller

public ActionResult Votation(PostedCandidates PostedCandidates)
{
    return View();
}

View

@Html.CheckBoxListFor(x => x.PostedCandidates.CandidatesId,  
                  x => x.AvailableCandidates,      
                  x => x.candidates_info_id,                    
                  x => x.candidates_fullname,                
                  x => x.SelectedCandidates)  

But when I tried to run this code, an error is displayed:

'System.Collections.Generic.IEnumerable' does not contain a definition for 'PostedCandidates' and no extension method 'PostedCandidates' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?)

4
  • Change the Parameter name of Votation Commented Mar 23, 2015 at 4:03
  • Post the code for whole view. I think the problem is with the code that you did not post here. Commented Mar 23, 2015 at 4:03
  • @model IEnumerable<Online_Ballot.Models.CandidatesViewModel> Commented Mar 23, 2015 at 4:07
  • I add this in my view:@model IEnumerable<Online_Ballot.Models.CandidatesViewModel>@using MvcCheckBoxList.Model Commented Mar 23, 2015 at 4:08

1 Answer 1

1

You are declaring model of type IEnumerable<Online_Ballot.Models.CandidatesViewModel> and using it like you are having an instance not a collection.

you either need to change your model declaration to following (assuming you are passing single instance of type CandidatesViewModel to view from controller):

@model Online_Ballot.Models.CandidatesViewModel

or you can change checkbox list generation to something like this (assuming you are passing collection to the view from controller and that collection is having only one element).

@Html.CheckBoxListFor(x => x.FirstOrDefault().PostedCandidates.CandidatesId,  
              x => x.FirstOrDefault().AvailableCandidates,      
              x => x.FirstOrDefault().candidates_info_id,                    
              x => x.FirstOrDefault().candidates_fullname,                
              x => x.FirstOrDefault().SelectedCandidates)

Although I would recommend you to go with change model declaration.

Update

I also noticed you are not passing instance of your view model to view.

public ActionResult Votation(PostedCandidates PostedCandidates)
{
    CandidatesViewModel vm = new CandidatesViewModel();
    //process or fill your viewmodel here.
    return View(vm);
}
Sign up to request clarification or add additional context in comments.

3 Comments

When I run the program, it returns a null value.
@JenalynParagados as my answer assumes you need to pass instance of your viewmodel to the view. CandidatesViewModel vm = new CandidatesViewModel(); and then pass instance of that viewmodel to view method like return view(vm);.
Is CheckBoxFor should be a boolean type value?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.