I am developing an ASP .Net MVC 3 application using C# and SQL Server 2005.
I am using also Entity Framework and Code First Method.
I want to upload data from a table (from my base) in a DropDownList.
The table is 'Poste'and it has a model 'Poste'.
The view showing the DropDownList is 'Gestion' and this its code :
<%@ 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 :")%></div>
<div><%:Html.Label("Position :")%></div>
<div><%:Html.Label("Poste Précédente :")%></div>
<div><%:Html.Label("Poste Suivante :")%></div>
</fieldset>
<% } %>
As you see, it is import from a FlowViewModel, which the code is :
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; }
}
}
and this is the controller which returns the view :
public class ProfileGaController : Controller
{
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);
}
The problem that an Exception appears in the execution which mentionne that the values are passing NULL :