0

I have Controller that looks like this:

public class PollController : BaseController
{
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public Task<IActionResult> AddVotingRecord(VotingRecordBindingModel model)
    {
        throw new NotImplementedException();
    }
}

My view is:

@model IEnumerable<SmartStoreNetCore.Web.Models.ViewModels.PollViewModel>

<div class="list-group-item text-muted title">COMMUNITY POLL</div>
@foreach (var poll in Model)
{
    <form asp-controller="Poll" asp-action="AddVotingRecord" method="post">
        <div class="list-group">            
            <div class="list-group-item title">@Html.Raw(poll.Name)</div>            
            @foreach (var pollAnswer in poll.PollAnswers)
            {
                <div class="list-group-item text-muted">
                    <input asp-for="@pollAnswer.VotingRecordBindingModel.PollAnswerId"
                       type="radio"
                       value="@pollAnswer.Id"/>
                @Html.Raw(pollAnswer.Name)
                </div>
            }
        <div class="list-group-item title">
            <input class="btn btn-warning btnCustom" type="submit" value="VOTE"/>
        </div>
    </form>
}

Where PollViewModel contains collection of PollAnswersViewModel where is the VotingRecordBindingModel which has property PollAnswerId.

The code above generates the right HTML and the values are the corect ids.

I am getting the VotingRecordBindingModel returned to the Controller but its property PollAnswerId is always 0.

I can't figure out what is going wrong.

EDIT:

I tried to add GET method AddVotingRecord() that returns new VotingRecordBindingModel() but without result.

Tseng
  • 61,549
  • 15
  • 193
  • 205
Kos
  • 567
  • 4
  • 15
  • 1
    The code does **not** generate the correct html (its generating `name` attribues that have no relationship to your model). You need to show your models. And show the html that your currently generating for the radio buttons –  Oct 25 '16 at 06:26
  • Also suggest you read [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) to understand how the `name` attribute needs to be –  Oct 25 '16 at 06:28
  • @StephenMuecke: Question is about ASP.NET Core which has tag helpers as preferred way rather than html helper – Tseng Oct 25 '16 at 06:37
  • Yep... That was it I added the line `name="PollAnswerId"` and it works fine. Thank you! – Kos Oct 25 '16 at 06:39
  • That's got nothing to do with it. Its the `name` attributes which are important (whether you use tag helpers or `HtmlHelper` method is irrelevant) - read the dupe and understand how the `name` attribute needs to be in relation to your model). –  Oct 25 '16 at 06:39
  • But your view makes no sense anyway - your generating a form for each item in the collection - but you can only submit one form. Apart from all the extra pointless html, the user could edit values in multiple forms and then click a submit button and not even realize that their edits have been lost. –  Oct 25 '16 at 06:41
  • There are two loops first one generates forms and the second one shows options to select. Each form has submit. – Kos Oct 25 '16 at 06:45
  • Yes, I know - and it makes no sense at all and is an awful UI experience for the user –  Oct 25 '16 at 07:02
  • I got what you mean and yes creating form for each item seems like bad idea. Probably I will display one form at a time or using AJAX to change the form after submitting... or something like this... Thanks for pointing this its going to save me some time ... I appreciate it! – Kos Oct 25 '16 at 18:54

0 Answers0