我的控制器看起来像这样:
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()
。