【问题标题】:Pass select/drop-down item's value to controller only receives 0将选择/下拉项的值传递给控制器​​仅接收 0
【发布时间】:2020-01-22 10:50:31
【问题描述】:

我已经成功创建了一个视图来显示一些投票记录。但是在同一个视图上,我尝试在我的控制器上调用另一个方法来导出这些记录,它只接收 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 错误

【问题讨论】:

    标签: c# asp.net-core controller entity-framework-core razor-pages


    【解决方案1】:

    Export 端点需要一个名为 electionYear 的表单值。但是您的&lt;select&gt; 没有那个name(或任何name)。试试:

    <select name="electionYear" class="form-control" asp-items="ViewBag.Election"></select>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多