您可以这么简单:
第一:例如,在Models中,您具有使用此实现的User.cs
public class User
{
public string username { get; set; }
public string age { get; set; }
}
我们正在将空模型传递给用户–当用户提交这样的表单时,该模型将被用户的数据填充
public ActionResult Add()
{
var model = new User();
return View(model);
}
当您将“按空用户查看”作为模型返回时,它会与您实现的表单的结构对应。我们在HTML方面有这个:
@model MyApp.Models.Student
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Student</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.username, htmlAttributes: new {
@class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.username, new {
htmlAttributes = new { @class = "form-
control" } })
@Html.ValidationMessageFor(model => model.userame, "",
new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.age, htmlAttributes: new { @class
= "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.age, new { htmlAttributes =
new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.age, "", new {
@class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default"
/>
</div>
</div>
</div>
}
所以在按钮提交时,您将像这样使用它
[HttpPost]
public ActionResult Add(User user)
{
}