0
總是出現錯誤,我無法弄清楚我的代碼的哪部分會產生此錯誤?我試圖將它與互聯網上的其他情況進行比較,但仍無法追蹤到發生的事情。有人可以與我分享你的建議嗎?MVC錯誤:未將對象引用設置爲@ Html.ValidationSummary上對象的實例
這是我的觀點:
@model StockroomMaitenance.Models.PG_User
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<p>
<a href="@Url.Action("Users", "Admin", new { id = Model.User_Id })" data-original-title="Back to List" data-toggle="tooltip">
<i class="glyphicon glyphicon-th-list"></i>Back to List</a>
</p>
<fieldset>
<legend>PG_User</legend>
<div class="editor-label">
@Html.LabelFor(model => model.User_Id)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.User_Id)
@Html.ValidationMessageFor(model => model.User_Id)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.User_BadgeId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.User_BadgeId)
@Html.ValidationMessageFor(model => model.User_BadgeId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.User_FullName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.User_FullName)
@Html.ValidationMessageFor(model => model.User_FullName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.User_Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.User_Email)
@Html.ValidationMessageFor(model => model.User_Email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.User_Role, "PG_Role")
</div>
<div class="editor-field">
@Html.DropDownList("User_Role", String.Empty)
@Html.ValidationMessageFor(model => model.User_Role)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.User_Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.User_Password)
@Html.ValidationMessageFor(model => model.User_Password)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
而且我的繼承人型號:
public partial class PG_User
{
public PG_User()
{
this.PG_UserAct = new HashSet<PG_UserAct>();
this.PG_Role1 = new HashSet<PG_Role>();
}
public int User_Id { get; set; }
public string User_BadgeId { get; set; }
public string User_FullName { get; set; }
public Nullable<int> User_Role { get; set; }
[Required(ErrorMessage = "Password is required")]
[DataType(DataType.Password)]
public string User_Password { get; set; }
public string User_Email { get; set; }
public virtual PG_Role PG_Role { get; set; }
public virtual ICollection<PG_UserAct> PG_UserAct { get; set; }
public virtual ICollection<PG_Role> PG_Role1 { get; set; }
}
這裏是我的控制器
public ActionResult Users(string sortUser, string searchString)
{
if (Session["LoggedUserRole"] == null && Session["LoggedUserFullname"] == null)
{
return RedirectToAction("Error", "Login");
}
else
{
ViewBag.NameSort = String.IsNullOrEmpty(sortUser) ? "User_FullName" : "";
ViewBag.BadgeSort = sortUser == "User_BadgeId" ? "user_desc" : "User_BadgeId";
var pg_user = db.PG_User.Include(p => p.PG_Role);
if (!String.IsNullOrEmpty(searchString))
{
pg_user = pg_user.Where(s => s.User_FullName.Contains(searchString));
if (pg_user != null)
{
pg_user = pg_user.Where(s => s.User_FullName.Contains(searchString));
}
else
{
ViewBag.NotFound = "No Results Found";
}
}
switch (sortUser)
{
case "User":
pg_user = pg_user.OrderByDescending(s => s.User_FullName);
break;
case "User_BadgeId":
pg_user = pg_user.OrderBy(s => s.User_BadgeId);
break;
case "user_desc":
pg_user = pg_user.OrderByDescending(s => s.User_BadgeId);
break;
default:
pg_user = pg_user.OrderBy(s => s.User_FullName);
break;
}
return View(pg_user.ToList());
}
}
//
// GET: /Admin/Details/5
public ActionResult Details(int id = 0)
{
if (Session["LoggedUserRole"] == null && Session["LoggedUserFullname"] == null)
{
return RedirectToAction("Error", "Login");
}
else
{
PG_User pg_user = db.PG_User.Find(id);
if (pg_user == null)
{
return HttpNotFound();
}
return View(pg_user);
}
}
//
// GET: /Admin/Create
public ActionResult Create()
{
if (Session["LoggedUserRole"] == null && Session["LoggedUserFullname"] == null)
{
return RedirectToAction("Error", "Login");
}
else
{
ViewBag.User_Role = new SelectList(db.PG_Role, "Role_Id", "Role_Description");
return View();
}
}
//
// POST: /Admin/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(PG_User pg_user)
{
if (Session["LoggedUserRole"] == null && Session["LoggedUserFullname"] == null)
{
return RedirectToAction("Error", "Login");
}
else
{
if (ModelState.IsValid)
{
db.PG_User.Add(pg_user);
db.SaveChanges();
return RedirectToAction("Users");
}
ViewBag.User_Role = new SelectList(db.PG_Role, "Role_Id", "Role_Description", pg_user.User_Role);
return View(pg_user);
}
}
//
// GET: /Admin/Edit/5
public ActionResult Edit(int id = 0)
{
if (Session["LoggedUserRole"] == null && Session["LoggedUserFullname"] == null)
{
return RedirectToAction("Error", "Login");
}
else
{
PG_User pg_user = db.PG_User.Find(id);
if (pg_user == null)
{
return HttpNotFound();
}
ViewBag.User_Role = new SelectList(db.PG_Role, "Role_Id", "Role_Description", pg_user.User_Role);
return View(pg_user);
}
}
聲明什麼問題?
可能重複[什麼是NullReferenceException,我該如何解決它?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-doi-i-fix-它) – 2015-03-25 09:14:16
不,我試圖訪問這一個。 – Anaiah 2015-03-25 09:36:42