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>