【发布时间】:2022-11-12 19:22:36
【问题描述】:
我知道下面的 post action 方法是反模式的,但我仍然假设看到一个新页面,其中 Name 设置为 null。但是当我点击提交按钮时,页面没有重新加载,我仍然看到显示的旧名称,这是浏览器的事情还是 asp.net 核心框架的事情?
public class HomeController : Controller
{
private IRepository repository;
public HomeController(IRepository repo)
{
repository = repo;
}
// ...
public IActionResult Create() // create a Employer that has a name in the browser
{
return View();
}
[HttpPost]
public IActionResult Create(Employee model)
{
model.Name = "";
return View(model);
}
}
// view file:
@model Employee
@{
ViewData["Title"] = "Create Employee";
}
<h2>Create Employee</h2>
<form asp-action="Create" method="post">
<div class="form-group">
<label asp-for="Id"></label>
<input asp-for="Id" class="form-control" />
</div>
<div class="form-group">
<label asp-for="Name"></label>
<input asp-for="Name" class="form-control" />
</div>
<div class="form-group">
<label asp-for="DOB"></label>
<input asp-for="DOB" class="form-control" />
</div>
<div class="form-group">
<label asp-for="Role"></label>
<select asp-for="Role" class="form-control" asp-items="@new SelectList(Enum.GetNames(typeof(Role)))"></select>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
【问题讨论】:
-
浏览器控制台中是否有任何错误?
标签: c# asp.net-core