C# 将选择/下拉项的值传递给控制器仅接收0
我已经成功创建了一个视图来显示来自votings的一些记录。但在同一个视图中,我尝试调用控制器上的另一个方法来导出这些记录,它只接收0作为参数 这是我的模型C# 将选择/下拉项的值传递给控制器仅接收0,c#,asp.net-core,controller,entity-framework-core,razor-pages,C#,Asp.net Core,Controller,Entity Framework Core,Razor Pages,我已经成功创建了一个视图来显示来自votings的一些记录。但在同一个视图中,我尝试调用控制器上的另一个方法来导出这些记录,它只接收0作为参数 这是我的模型 public class Vote { public int Id { get; set; } public int CPF { get; set; } public virtual Candidate Candidate { get; set; } public in
public class Vote
{
public int Id { get; set; }
public int CPF { get; set; }
public virtual Candidate Candidate { get; set; }
public int CandidateID { get; set; }
public DateTime Moment { get; set; }
}
这是我的索引
public async Task<IActionResult> Index()
{
var context = _context.Vote.Include(v => v.Candidate).Include(v => v.Candidate.Election);
//This is going to be on the view for the user to select the year to export data
ViewData["Election"] = new SelectList(_context.Election, "Id", "Year");
return View(await context.ToListAsync());
}
这是我用来发布到控制器的表单
@model IEnumerable<Models.Vote>
...
//Here i show the votes and then below i show this form with the Elections
<form asp-action="Export">
<div class="form-group">
<select class="form-control" asp-items="ViewBag.Election"></select>
</div>
<div class="form-group">
<input type="submit" value="Exportar" class="btn btn-default" />
</div>
</form>
我用[FormBody]尝试了它,它给了我415个错误导出端点需要一个名为electionYear的表单值。但你没有这个名字或任何名字。尝试:
@model IEnumerable<Models.Vote>
...
//Here i show the votes and then below i show this form with the Elections
<form asp-action="Export">
<div class="form-group">
<select class="form-control" asp-items="ViewBag.Election"></select>
</div>
<div class="form-group">
<input type="submit" value="Exportar" class="btn btn-default" />
</div>
</form>
<select name="electionYear" class="form-control" asp-items="ViewBag.Election"></select>