【发布时间】:2021-04-22 13:47:16
【问题描述】:
我在 Employee 控制器中创建了一个用于创建新 Employee 的方法:
[HttpGet]
[ActionName ("Create")]
public ActionResult Create()
{
return View();
}
我添加了一个带有员工(强类型)模型的 Create 方法的视图,并使用了 create 模板。 当我运行程序并单击新建时,我在创建视图中收到空引用错误。
@model BuissnessLayer.Employee
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Employee</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Employee_Id, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Employee_Id, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Employee_Id, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Employee_Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Employee_Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Employee_Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Employee_Age, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Employee_Age, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Employee_Age, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Employee_City, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Employee_City, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Employee_City, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Employee_Salary, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Employee_Salary, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Employee_Salary, "", 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>
<script src="~/Scripts/jquery-3.4.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
我在行得到错误
@Html.EditorFor(model => model.Employee_Id, new { htmlAttributes = new { @class = "form-control" } })
我不知道如何解决这个问题,因为代码是自动创建的,我还没有编辑任何东西。
【问题讨论】:
-
这可能不是您看到异常的原因,但您确定不想在控制器中使用
HttpPost而不是HttpGet? -
您需要将模型传递给
return View(); -
@melancia 我有两个方法 httpget 和 httppost 分别具有相同的操作名称。
-
当没有模型传入时,您期望
model.Employee_Id是什么(即model是null)? -
@phuzi 谢谢它的工作。我正在关注 youtube(youtube.com/watch?v=qpAe45xTEfQ) 的教程,他没有在代码中传递型号名称,但他的代码有效。
标签: c# html asp.net-core model-view-controller