c# - 创建视图后未将对象引用设置为对象的实例
问题描述
到目前为止,我有一个非常简单的程序,这意味着找到问题应该不会太难。
当我加载网站时,我在注册视图的第 21 行收到以下错误:
@Html.EditorFor(model => model.EmployeeId, new { htmlAttributes = new { @class = "form-control" } })
System.NullReferenceException:“对象引用未设置为对象的实例。”
模型类
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
namespace MVCApp.Models
{
public class EmployeeModel
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
public string ConfirmEmail { get; set; }
public string Password { get; set; }
public string ConfirmPassword { get; set; }
}
}
控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVCApp.Controllers
{
public class HomeController : Controller
{
...
public ActionResult SignUp()
{
ViewBag.Message = "Employee Sign Up";
return View();
}
}
}
视图(自动生成)
@model MVCApp.Models.EmployeeModel
@{
ViewBag.Title = "Employee Sign Up";
}
<h2>SignUp</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>EmployeeModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.EmployeeId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EmployeeId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EmployeeId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EmailAddress, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EmailAddress, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EmailAddress, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ConfirmEmail, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ConfirmEmail, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ConfirmEmail, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ConfirmPassword, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ConfirmPassword, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ConfirmPassword, "", 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>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
我正在使用 Microsoft Visual Studio Community 2019 版本 16.7.2 和 .NET Framework 4.7.2
谢谢
解决方案
推荐阅读
- sql - 当特定参考存在多行时返回单行
- wiremock - 使用 WireMock 时查询参数不匹配
- windows - 我需要快速删除空文件夹和子文件夹
- javascript - 如何使用 Javascript 解决多个粘滞标题问题?
- master-slave - 使用sr触发器可以开发主从触发器吗?
- c# - C# 在 ScriptErrorsSuppressed 属性为 False 的 Web 浏览器中打开弹出窗口
- javascript - 用 promt 和 for 循环写出一个数组
- javascript - 使用 Typescript 在 JSON 中搜索字符串
- ruby-on-rails - has_and_belongs_to_many 的联接表未迁移
- unity3d - 正确处理对象状态