我通过 mvc 的自动化方式为模型创建了一个简单的创建视图,以向同事展示。现在,虽然我并没有真正使用它,但我看不到递归是在哪里引起的,因为我只是对 mvc 说要为一个模型创建它,其中所有字段都是用 "public 'type' 'name' { get ; 设置;}" 格式。我将在下面包含模型、操作和视图。这对我来说不是问题,因为我根本不使用模板,但我很好奇在没有插入自定义代码时会发生这种情况
提前致谢
控制器动作
public ActionResult View()
{
return View();
}
模型
namespace Data_Access.Models
{
public class StudentModel
{
public int studentId { get; set; }
public string name { get; set; }
public string surname { get; set; }
public string classroom { get; set; }
public string role { get; set; }
public string imgPath { get; set; }
}
}
最后是视图
@model Data_Access.Models.StudentModel
@{
ViewBag.Title = "View";
}
<h2>View</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>StudentModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.studentId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.studentId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.studentId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.surname, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.surname, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.surname, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.classroom, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.classroom, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.classroom, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.role, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.role, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.role, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.imgPath, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.imgPath, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.imgPath, "", 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>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>