0 c# asp.net-mvc asp.net-core-mvc
我有一个这样的约会类:
public class Appointment
{
public string ClientName { get; set; }
public DateTime Date { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在HomeController:
public IActionResult Index()
{
return View(new Appointment { Date = DateTime.Now }); //first time to get the form
}
Run Code Online (Sandbox Code Playgroud)
并且默认index.cshtml内部有一个表单并显示客户端名称和日期,当按下提交按钮时,它会将值发布到MakeBooking操作方法,如下所示:
public ViewResult MakeBooking(Appointment appt)
{
return View(); // second time to get the form
}
Run Code Online (Sandbox Code Playgroud)
我不明白的是,我运行了应用程序并在表单中填写了一些值,例如客户名称是“迈克尔”,日期是“20/08/2019”并按下了提交按钮,它被定向到MakeBookingaction 方法,在 action 方法中,我返回了一个没有数据模型的视图。所以第二次,表单应该没有价值,因为我没有appt在视图中传递数据模型,但为什么我仍然在视图中填充了“迈克尔”和“20/08/2019”?
这是index.cshtml:
@model Appointment
@{ Layout = "_Layout"; }
<form class="m-1 p-1" asp-action="MakeBooking" method="post">
<div class="form-group">
<label asp-for="ClientName">Your name:</label>
<input asp-for="ClientName" class="form-control" />
</div>
<div class="form-group">
<label asp-for="Date">Appointment Date:</label>
<input asp-for="Date" type="text" asp-format="{0:d}" class="form-control" />
</div>
<button type="submit" class="btn btn-primary">Make Booking</button>
</form>
Run Code Online (Sandbox Code Playgroud)
小智 5
Html Helpers 实际上在 ModelState 中检查要在字段中显示的值,然后再查看模型。在控制器中使用它
ModelState.Clear();
return View();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
433 次 |
| 最近记录: |