【发布时间】: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