在ASP MVC中打开页面时出现NullException

时间:2013-05-13 08:00:01

标签: c# sql-server asp.net-mvc-3 visual-studio-2010

我正在使用C#和SQL Server 2005开发ASP.Net MVC 3应用程序。

我创建了一个按钮,当它被点击时会显示一个字段集(并且使用Jquery影响向下滑动)。

字段集包含表单。

当我在这个表单中创建DropDownList时,我从FormViewModel调用了一个列表(因为我使用的是Entity Framework和Code First方法)。

问题是:当我打开页面时,会出现此异常:

NullReferenceException was unhadled by user code. Object reference not set to an instance of an object.

它与'PostesItems'有关,我不知道为什么他要求声明该对象,因为我从viewModel导入它。

这是视图的代码:

<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcApplication2.Models.FlowViewModel>" %>




<% using (Html.BeginForm()) { %>
    <%: Html.ValidationSummary(true) %>
    <fieldset class="parametrage">
        <legend>Gestion de Gamme</legend>

        <div><%:Html.Label("Poste :")%><%: Html.DropDownList("SelectedPoste", new SelectList(Model.PostesItems, "ID_Poste", "ID_Poste"))%></div>


         <div><%:Html.Label("Nombre de Passage :")%><%: Html.DropDownList("SelectedGamme", new SelectList(Model.GaItems,"Nbr_Passage","Nbr_Passage") )%></div>




        </fieldset>
         <% } %>

这就是我调用的FlowViewModel:

namespace MvcApplication2.Models
{
    public class FlowViewModel
    {
        [Key]
        public string IDv { get; set; }
        public List<Poste> PostesItems { get; set; }
        public List<Profile_Ga> Profile_GaItems { get; set; }
        public List<Gamme> GaItems { get; set; }

        public int SelectedProfile_Ga { get; set; }

        public int SelectedGamme{ get; set; }

        public int SelectedPoste { get; set; }
    }
}

这是显示视图的控制器:

private GammeContext db = new GammeContext();

        //
        // GET: /ProfileGa/
        [HttpGet]
        public ActionResult Index(Profile_Ga profile_ga, Poste poste)
        {

            var viewModel = new FlowViewModel();
            viewModel.PostesItems = db.Postes.ToList();
               viewModel.Profile_GaItems = db.Profil_Gas.ToList();
               viewModel.GaItems = db.Gammes.ToList();
               return View(viewModel);



        }

这是Poste模型:

namespace MvcApplication2.Models
{
    public class Poste
    {
        [Required]
        [Key]
        [Display(Name = "ID Poste :")]
        public string ID_Poste { get; set; }

        [Required]
        [Display(Name = "Nom Poste:")]
        public string nom_Poste { get; set; }

        [Required]
        [Display(Name = "Application :")]
        public string Application { get; set; }

        [Required]
        [Display(Name = "In Poste :")]
        public string In_Po { get; set; }

        [Required]
        [Display(Name = "Out Poste :")]
        public string Out_Po { get; set; }

        [Required]
        [Display(Name = "Etat :")]
        public string Etat { get; set; }

        [Required]
        [ForeignKey("Ligne")]
        [Display(Name = "ID Ligne :")]
        public string ID_Ligne { get; set; }

        [Required]
        [Display(Name = "Mouvement :")]
        public string Mouvement { get; set; }

        public virtual Ligne Ligne { get; set; }
        public IEnumerable<Ligne> Lignes { get; set; }

        public virtual ICollection<Poste> Postes { get; set; }
    }

这就是Gamme模型:

public class Gamme
    {
        [Key]
        [Column(Order = 0)]
        [ForeignKey("Profile_Ga")]
        public string ID_Gamme { get; set; }
        [Key]
        [Column(Order = 1)]
        [ForeignKey("Poste")]
        public string ID_Poste { get; set; }
        public int Position { get; set; }
        public int Nbr_Passage { get; set; }
        public string Last_Posts { get; set; }
        public string Next_Posts { get; set; }

        public virtual Poste Poste { get; set; }
        public virtual Profile_Ga Profile_Ga { get; set; }

    } }

错误: error

2 个答案:

答案 0 :(得分:2)

由于错误提示您的PostesItems为空。 EntityFramework使用延迟加载方法,这意味着如果没有命令它将不加载相关实体。

加载帖子的方法之一是:

FlowViewModel model = db.FlowViews... // loading view model
model.PostesItems = db.PostesItems.ToList();

更多信息about lazy loading here

答案 1 :(得分:1)

根据聊天中的讨论:

在您的索引视图中,有一个js函数可以加载来自另一个控制器操作的内容。

您的问题出在AnnouarController。您的方法Gestion()返回带有FlowViewModel类型模型的View,但模型未初始化。 你至少应该改为:

         public ActionResult Gestion()
         {
               var viewModel = new FlowViewModel();
               viewModel.PostesItems = new SelectList(db.Postes.ToList(), "ID_Poste", "ID_Poste"); 
               return View(viewModel);
         }