对象引用未设置为Entity Framework中对象的实例

时间:2018-02-15 10:47:13

标签: asp.net-mvc-5 frameworks entity

我对这个错误有疑问,我无法解决。我首先使用Entity Framework数据库。在此之前,我删除了实体模型并创建了一个新模型。我不确定它是否会影响。

这是我的viewModel

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

namespace MVCTutorial.Models
{
public class OtherIncViewModel
{
    public virtual DbSet<User> Users { get; set; }
    public virtual DbSet<UserGroup> UserGroups { get; set; }
    public virtual DbSet<Patient> Patients { get; set; }

    //User
    public int UserId { get; set; }
    public string UserFullName { get; set; }
    public string UserIC { get; set; }
    [DataType(DataType.Password)]
    [Required(ErrorMessage = "This field is requiered.")]
    public string UserPassword { get; set; }
    public string UserEmail { get; set; }
    public Nullable<int> UserGroupId { get; set; }
    public string UserMMCRegistrationNo { get; set; }
    [DisplayName("UserName")]
    [Required(ErrorMessage = "This field is requiered.")]
    public string UserLogin { get; set; }
    public string UserFaxNo { get; set; }
    public string UserDesignation { get; set; }
    public string UserStatus { get; set; }
    public string UserContact { get; set; }
    public virtual UserGroup UserGroup { get; set; }
    public string LoginErrorMessage { get; set; }

    //Patient
    public int PatientId { get; set; }
    public string PatientName { get; set; }
    public string PatientIC { get; set; }
    public int PatientAge { get; set; }
    public string PatientGender { get; set; }
    public string Hospital { get; set; }
    public string Ward { get; set; }
    public string Department { get; set; }
    public string Barcode { get; set; }
    public string Race { get; set; }
    public string PatientErrorMessage { get; set; }
    public virtual ICollection<CaseOtherInc> CaseOtherIncs { get; set; }
    public virtual ICollection<DraftCaseOtherInc> DraftCaseOtherIncs { get; set; }

}
}

这是我的控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCTutorial.Models;


namespace MVCTutorial.Controllers
{
public class WardStaffController : Controller
{
    OtherIncContext db = new OtherIncContext();
    // GET: WardStaff
    public ActionResult Index() //Ward Staff Inbox
    {
        return View();
    }

    public ActionResult CreateTransfusion() //Ward Staff Create Transfusion
    {
        return View();
    }

    public ActionResult CreateIBCT() //Ward Staff Create IBCT
    {
        return View();
    }

    public ActionResult CreateNearMiss() //Ward Staff Create Near Miss
    {
        return View();
    }

    [HttpPost] 
    public ActionResult SearchPatient(MVCTutorial.Models.OtherIncViewModel patient)
    {

        using (OtherIncContext db = new OtherIncContext())
        { //I am wondering here why my local variable does not reference the namespace in this case is MVCTutorial. Is this why i get the null exception? I have check my database...i didn't have null record.
            var patientDetails = db.Patients.Where(x => x.PatientIC == patient.PatientIC).FirstOrDefault(); 

            if (patientDetails == null)
            {
                patient.PatientErrorMessage = "Please enter patient's IC number.";
                return View("CreateOtherIncident", patient);
            }
            else
            {

                Session["PatientName"] = patientDetails.PatientName;
            }
            return RedirectToAction("CreateOtherIncident", "WardStaff");
        }

    }

这是我的观点

@model MVCTutorial.Models.OtherIncViewModel
@{
ViewBag.Title = "CreateOtherIncident";
Layout = "~/Views/Shared/_LayoutWardStaff.cshtml";
}

<h2>CreateOtherIncident</h2>

<h2>Reported By:</h2>

<p> Name: @Session["UserFullName"].ToString()</p>
<p>Email Address: @Session["UserEmail"].ToString()</p>
<p>Date:</p>
<p>Designation No.: @Session["UserDesignation"].ToString()</p>
<p>Tel.No: @Session["UserContact"].ToString()</p>
<p>Fax.No: @Session["UserFaxNo"].ToString()</p>

<h2>Patient Details:</h2>

@Html.LabelFor(model=>model.PatientIC)
@Html.EditorFor(model=>model.PatientIC)

<p>Patient Name: @Session["PatientName"].ToString()</p> //I get the exception here.

我怀疑是因为局部变量,但我找不到任何进一步的错误或异常。

1 个答案:

答案 0 :(得分:0)

在异常之前,要检查会话不应该为空。

<强>像:

@if (HttpContext.Current.Session["PatientName"] != null)
            { //@Session["PatientName"].ToString()
}