Hello.
I am working on a simple polling system.
I have 2 tables :
Polls:
Id,Title
PollOptions:
Id, PollId, Title, VoteCount
And a ViewModel :
public Poll Poll { get; set; } public List<PollOption> PollOptions { get; set; }
So in a test view i want to get current Poll and select one option and submit it:
public async Task<IActionResult> Test() { var poll = db.Polls.Find(1); var polloptions = db.PollOptions.Where(p => p.PollId == poll.Id); var pollviewmodel = new PollViewModel() { Poll = poll, Options = polloptions }; return View(pollviewmodel ); }
in View :
@model PollViewModel;<div class="container">
@Model.Poll.Title<br/>
@for(int i = 0; i < Model.PollOptions.Count; i++)
{<p><input asp-for="@Model.PollOptions[i].Id" value="@Model.PollOptions[i].Id" type="radio"/>
<label asp-for="@Model.PollOptions[i].Id">@Model.PollOptions[i].Title</label> <input asp-for="@Model.PollOptions[i].Id" type="hidden"/> </p> } </div>
So in this code if i run all items will show as selected and if i change this line :
<input asp-for="@Model.PollOptions[i].Id" value="@Model.PollOptions[i].Id" type="radio"/>
to this :
<input asp-for="@Model.PollOptions[i]" value="@Model.PollOptions[i].Id" type="radio"/>
none item will be selected and also i can not select any of them.
i want to show all options and user select one and submit form and add 1 to ViewCount field.
Rendered result :
<div class="container"> Poll Title<br/><p><input value="1" type="radio" checked="checked" data-val="true" data-val-required="The Id field is required." id="PollOptions_0__Id" name="PollOptions[0].Id"><label for="PollOptions_0__Id">Option1</label><input type="hidden" id="PollOptions_0__Id" name="PollOptions[0].Id" value="1"></p><p><input value="2" type="radio" checked="checked" data-val="true" data-val-required="The Id field is required." id="PollOptions_1__Id" name="PollOptions[1].Id"><label for="PollOptions_1__Id">Option2</label><input type="hidden" id="PollOptions_1__Id" name="PollOptions[1].Id" value="2"></p><p><input value="3" type="radio" checked="checked" data-val="true" data-val-required="The Id field is required." id="PollOptions_2__Id" name="PollOptions[2].Id"><label for="PollOptions_2__Id">Option3</label><input type="hidden" id="PollOptions_2__Id" name="PollOptions[2].Id" value="3"></p></div>
please help me i am so confused :-(