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 have a view Index which contains some links to othres views.
Among those views, there is 'Update.aspx' which i created (not generated automaticalu by Entity Framework).
Its object is to edit some values in a table 'Gamme' in my base.
The problem is that : when I click on the link of this view (Update), this error appears :
Object reference not set to an instance of an object.
This a part of the code of the view Index which contain the link for the view Upadate :
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.master" Inherits="System.Web.Mvc.ViewPage<MvcApplication2.Models.FlowViewModel>" %>
<%@ Import Namespace="Helpers" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<fieldset>
<legend>Gestion des Gammes</legend>
<table>
<tr>
<th>
ID Gamme
</th>
<th>
ID Poste
</th>
<th>
Nombre de Passage
</th>
<th>
Position
</th>
<th>
Poste Précédent
</th>
<th>
Poste Suivant
</th>
<th>Opérations</th>
</tr>
<% foreach (var item in Model.GaItems) { %>
<tr>
<td>
<%: Html.DisplayFor(modelItem => item.ID_Gamme) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.ID_Poste) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Nbr_Passage) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Position) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Last_Posts) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Next_Posts) %>
</td>
<td>
<%: Html.ActionLink("Modifier", "Update", new { id=item.ID_Gamme }) %>
</td>
</tr>
<% } %>
</table>
</fieldset>
</asp:Content>
and this a part of the code of the controller ProfileGa :
public ActionResult Update(string id)
{
FlowViewModel flv = db.FlowViewModels.Find(id);
return View(flv);
}
[HttpPost]
public ActionResult Update(FlowViewModel model)
{
Console.WriteLine("" + model.Nbr_Passage);
Gamme G = new Gamme();
ListG.Remove(G);
db.Gammes.Remove(G);
G.ID_Gamme = model.SelectedProfile_Ga;
G.ID_Poste = model.SelectedPoste;
G.Last_Posts = model.PostePrecedentSelected;
G.Next_Posts = model.PosteSuivantSelected;
G.Nbr_Passage = int.Parse(model.Nbr_Passage);
G.Position = int.Parse(model.Position);
ListG.Add(G);
db.Gammes.Add(G);
db.SaveChanges();
return RedirectToAction("Index");
}
and this is the code of the model FlowViewModel :
public class FlowViewModel
{
[Key]
public string IDv { get; set; }
[NotMapped]
public SelectList PostesItems { get; set; }
public List<Profile_Ga> Profile_GaItems { get; set; }
public List<Gamme> GaItems { get; set; }
public string SelectedProfile_Ga { get; set; }
public string SelectedPoste { get; set; }
public string PostePrecedentSelected { get; set; }
public string PosteSuivantSelected { get; set; }
public string Position { get; set; }
public string Nbr_Passage { get; set; }
public List<Gamme> ListG = new List<Gamme>();
}
and finaly the code of the View 'UPDATE' :
asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Update
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Modifier une gamme</h2>
<script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>" type="text/javascript"></script>
<% using (Html.BeginForm()) { %>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Gamme</legend>
<div><%:Html.Label("Gamme :")%><%: Html.DropDownListFor(model => model.SelectedProfile_Ga, new SelectList(Model.Profile_GaItems, "ID_Gamme", "ID_Gamme"))%></div>
<div><%:Html.Label("Poste :")%><%: Html.DropDownListFor(model => model.SelectedPoste, Model.PostesItems)%></div>
<div><%:Html.Label("Nombre de Passage :")%><%: Html.EditorFor(model=>model.Nbr_Passage)%></div><div class="editor-field"> <%: Html.ValidationMessageFor(model => model.Nbr_Passage)%></div>
<div><%:Html.Label("Position :")%><%: Html.EditorFor(model=>model.Position)%></div>
<div><%:Html.Label("Poste Précédent :")%><%: Html.DropDownListFor(model => model.PostePrecedentSelected, Model.PostesItems)%></div>
<div><%:Html.Label("Poste Suivant :")%><%: Html.DropDownListFor(model => model.PosteSuivantSelected, Model.PostesItems)%></div>
<p>
<input type="submit" value="Enregistrer" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Reour à la liste", "Index") %>
</div>