-1

I'm trying to Create Poll System but there some issue. I have to classes generate it by ADO.NET Entity Framework Second I create Repository class to work with it from my controller class I create ActionResult Method in my PollController class that return All the Pool Questions from my DataBase.

I have one Particle View I create it to show the option for specific question When I put this particle in my Index.cshtml it give me this error The model item passed into the dictionary is of type 'System.Collections.Generic.List

here is my code Poll.cs

public partial class Poll
    {
        public Poll()
        {
            this.PollOptions = new HashSet<PollOption>();
        }

        public int PollID { get; set; }
        public Nullable<System.DateTime> AddedDate { get; set; }
        public string AddedBy { get; set; }
        public string QuestionText { get; set; }
        public bool IsCurrent { get; set; }
        public bool IsArchived { get; set; }

        public virtual ICollection<PollOption> PollOptions { get; set; }
    }

PollOption.cs

public partial class PollOption
    {
        public int OptionID { get; set; }
        public Nullable<System.DateTime> AddedDate { get; set; }
        public string AddedBy { get; set; }
        public string OptionText { get; set; }
        public int PollID { get; set; }
        public Nullable<int> Votes { get; set; }

        public virtual Poll Poll { get; set; }
    }

PollRepository.cs

public class PollRepository
    {
        private PollPlatFormEntities entities = new PollPlatFormEntities();

        public IQueryable<Poll> GetPolls()
        {
            return entities.Polls;
        }

        public Poll GetPoll(int Id)
        {
            return entities.Polls.FirstOrDefault(p => p.PollID == Id);
        }

        public IQueryable<Poll> CurrentPoll()
        {
            return entities.Polls.Where(c => c.IsCurrent == true);
        }

        public PollOption GetPollOption(int Id)
        {
            return entities.PollOptions.FirstOrDefault(o => o.OptionID == Id);
        }

PollController.cs

public class PollsController : Controller
    {
        PollRepository pollRepository = new PollRepository();
        //
        // GET: /Polls/
        public ActionResult Index()
        {
            var polls = pollRepository.GetPolls().ToList();
            return View(polls);
        }
}

here is my index.cshtml View

@model IEnumerable<PollSystem.Models.Poll>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.QuestionText)
            @Html.Partial("PollItem") 
        </td>
    </tr>
}

and lastly my Particle View

@model PollSystem.Models.Poll

<div id="poll-@Model.PollID" class="poll">
    <h2>@Model.QuestionText</h2>

    @using (Html.BeginForm(FormMethod.Post))
    {
        <ul class="poll-options">
            @foreach (var option in Model.PollOptions)
            {
                <li class="option" id="option-@option.OptionID">
                    <input type="radio" id="option-@option.OptionID" value="@option.OptionID"/>
                    <label class="text" for="option-@option.OptionID">
                        @option.OptionText
                    </label>
                </li>
            }
        </ul>
        <button type="submit" name="poll-submit">Vote</button>
    }
</div>
Mohammed
  • 13
  • 4

2 Answers2

0

If you do not explicitly pass a model to the partial view, it will use the model of the parent view, from which the partial view is called. In your case, you are not passing any model to the partial view method call, hence it is using the model of the parent page which is IEnumerable<PollSystem.Models.Poll> as the model of the partial.

Use this overload of Partial method which takes the model of the partial view as the second parameter

public static MvcHtmlString Partial(
    this HtmlHelper htmlHelper,
    string partialViewName,
    object model
)

So in your main view, you can pass the item variable to the Partial call

@model IEnumerable<PollSystem.Models.Poll>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.Partial("PollItem",item) 
        </td>
    </tr>
}
Shyju
  • 214,206
  • 104
  • 411
  • 497
0

Please try

 @model IEnumerable<PollSystem.Models.Poll>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.QuestionText)
            @Html.Partial("PollItem",item) 
        </td>
    </tr>
}
hakantopuz
  • 481
  • 5
  • 11