输入表单不将值绑定到返回的模型

时间:2016-10-25 06:20:29

标签: c# razor asp.net-core asp.net-core-mvc

我的控制器看起来像这样:

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

我的观点是:

@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>
}

其中PollViewModel包含PollAnswersViewModel的集合,其中VotingRecordBindingModel的属性为PollAnswerId

上面的代码生成了正确的HTML,值是核心ID。

我将VotingRecordBindingModel返回给Controller,但其属性PollAnswerId始终为0

我无法弄清楚出了什么问题。

修改

我尝试添加返回AddVotingRecord()但没有结果的GET方法new VotingRecordBindingModel()

0 个答案:

没有答案