【问题标题】:NullException appears when the page is opened in ASP MVC在 ASP MVC 中打开页面时出现 Null Exception
【发布时间】:2013-05-14 02:30:53
【问题描述】:

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

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

字段集包含一个表单。

当我在这个表单中创建了一个 DropDownList 时,我从 FormViewModel 中调用了一个列表(因为我使用的是实体框架和代码优先方法)。

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

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; }
    }

这是游戏模型:

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; }

    } }

错误:

【问题讨论】:

  • 你如何填充PostesItems
  • 您能显示返回此视图的控制器操作吗?
  • 我的基础中有一个 Poste 表,,,,使用实体框架,,,它将像列表一样填充,,,请看我的编辑,,,我将放置控制器
  • @KirillBestemyanov 当然,,,看看我的编辑
  • 能否在 viewModel.PostesItems = db.Postes.ToList() 之后放置断点?看看这个属性是什么?

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


【解决方案1】:

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

加载帖子的一种方法是:

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

更多信息about lazy loading here

【讨论】:

  • 谢谢回答,,,我想我是在我的控制器中做到的,,,你能看看我的编辑
  • 我还卡在这里,你能陪我看看吗?
  • 我正在寻找,我看不出你的代码有什么问题。这是您发布的完整 html 视图代码吗?
  • 这是我希望在单击按钮时显示的视图的完整 html 代码
  • gzaxx - 我原以为 ToList 会导致立即执行,将 db.PostesItems 变成操作中的列表,而不是视图中的列表?
【解决方案2】:

基于聊天中的讨论:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 2018-03-20
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多