【问题标题】:getting null reference in create view在创建视图中获取空引用
【发布时间】: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 是什么(即modelnull)?
  • @phuzi 谢谢它的工作。我正在关注 youtube(youtube.com/watch?v=qpAe45xTEfQ) 的教程,他没有在代码中传递型号名称,但他的代码有效。

标签: c# html asp.net-core model-view-controller


【解决方案1】:

您应该将模型传递给您的视图..

[HttpGet]
[ActionName ("Create")]
public ActionResult Create()
{
    var model = new Employee(){}
    return View(model);
}

@model BuissnessLayer.Employee

【讨论】:

  • 谢谢你成功了。我已经创建了员工实例并将其传递到视图中这样做是不是很糟糕?
猜你喜欢
  • 2011-08-13
  • 2019-08-05
  • 2011-07-31
  • 1970-01-01
  • 1970-01-01
  • 2013-04-02
  • 1970-01-01
  • 2016-06-13
  • 1970-01-01
相关资源
最近更新 更多