【发布时间】:2019-07-22 14:42:22
【问题描述】:
我正在尝试找出在控制器中创建提交和添加按钮操作的最佳方法。
我有一个用于创建(提交)的 HttpGet,但不确定如何执行 HttpPost 或者是否需要 Get 或 Post:
[HttpGet]
public IActionResult Create()
{
var drafList = _drService.GetDraft().ToList();
var IndexViewModel = new IndexViewModel();
IndexViewModel.Draft = draftList;
IndexViewModel.Published = _drService.GetPublished();
IndexViewModel.Current = _drService.GetCurrent();
return View(IndexViewModel);
}
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text- danger"></div>
<div class="form-group">
<div class="col-md-3">
<label for="asof">As of:</label>
</div>
<div class="col-md-9">
<input name="AsOf" type="date" title="AsOf" class="form-control" />
</div>
</div>
<div class="clearfix col-md-12"></div>
<div class="clearfix col-md-12"></div>
<div class="form-group">
<div class="col-md-2">
<label for="title">Title:</label>
</div>
<div class="col-md-9 col-md-offset-1">
<input type="text" class="form-control" id="title" />
</div>
<div class="col-md-6">
<input type="submit" value="Add" class="btn btn-primary" />
</div>
</div>
</form>
</div>
</div>
我希望在单击“添加”按钮时执行控制器中的操作并添加记录。
【问题讨论】:
-
您的输入框需要使用
asp-for。参考docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/…
标签: asp.net-core model-view-controller