将 select/drop-down 项的值传递给控制器仅接收 0
Pass select/drop-down item's value to controller only receives 0
我已经成功创建了一个视图来显示一些投票记录。但是在同一个视图上,我尝试在我的控制器上调用另一个方法来导出这些记录,它只接收 0 作为参数。
这是我的模型。
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());
}
这就是我要调用的操作。
[HttpPost]
public IActionResult Export(int electionYear)
这是我用来 POST 给控制器的表格。
@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 错误
Export
端点需要一个名为 electionYear
的表单值。但是您的 <select>
没有 name
(或任何 name
)。尝试:
<select name="electionYear" class="form-control" asp-items="ViewBag.Election"></select>
我已经成功创建了一个视图来显示一些投票记录。但是在同一个视图上,我尝试在我的控制器上调用另一个方法来导出这些记录,它只接收 0 作为参数。
这是我的模型。
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());
}
这就是我要调用的操作。
[HttpPost]
public IActionResult Export(int electionYear)
这是我用来 POST 给控制器的表格。
@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 错误
Export
端点需要一个名为 electionYear
的表单值。但是您的 <select>
没有 name
(或任何 name
)。尝试:
<select name="electionYear" class="form-control" asp-items="ViewBag.Election"></select>