anouar
anouar

Reputation: 317

How to save values in a list in ASP MVC 3

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 Form in a Partial View which the code is :

<%@  Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcApplication2.Models.FlowViewModel>" %>
<% using (Html.BeginForm("Save", "ProfileGa")) { %>
    <%: Html.ValidationSummary(true) %>
    <fieldset class="parametrage">
        <legend>Gestion de Gamme</legend>

        <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 Suivant :")%><%: Html.DropDownListFor(model => model.PosteSuivantSelected, Model.PostesItems, new { @id = "dd" })%>

        </div>


        <div>
        <input type="submit" value="Enregistrer" id="btnSave"  />
        </div>

        </fieldset>

and this is how it looks : pic I want to save the values entred in a List. So I created a list in my view model like this :

public List<Gamme> ListG = new List<Gamme>();

Then in my Controller 'ProfileGaController', I defined a method save to save in the list :

[HttpPost]
    public ActionResult Save(FlowViewModel model)
    {

        if (ModelState.IsValid)
        {
            Gamme G = new Gamme();
            G.ID_Gamme = model.SelectedProfile_Ga;
            G.ID_Poste = model.SelectedPoste;

            G.Next_Posts = model.PosteSuivantSelected;
            G.Nbr_Passage = int.Parse(model.Nbr_Passage);
            G.Position = int.Parse(model.Position);
            model.ListG.Add(G);
        }
        return RedirectToAction("Index");    
    }

Problem :

Each time I a save in the list, th values are deleted and replaced by new ones.

Always, I have count=1 for the list :

pp

Upvotes: 1

Views: 4091

Answers (2)

Colm Prunty
Colm Prunty

Reputation: 1620

I'm not sure why you need this list at all. The Model only exists for the duration of this controller action, and you're not passing it on to the view. In your code, you're making a new Gamme, adding it to a list, and then throwing the whole thing away. If you're just trying to save a new Gamme in the database, does it not work if you do

db.Gammes.Add(G);
db.SaveChanges();

instead of

model.ListG.Add(G);

What's the purpose of the list?

Edited after further clarification:

It seems like you want a temporary list of Gamme items added in one session, so you can use a session variable to store a list of Gammes you've added at that time. Initialise in the controller and add in the save action like so:

((List<Gamme>)System.Web.HttpContext.Current.Session["GammeList"]).Add(G);

Then if you want to assign it to a parameter of the View Model it's like:

model.ListG = (List<Gamme>)System.Web.HttpContext.Current.Session["GammeList"];

I, personally, would go back and engineer this slightly differently. I would put a bool on Gamme called "processed" or something similar, and when you create them, set it to false. That way, instead of using a session variable list, you can get the same list by selecting all Gamme objects where processed is false.

Upvotes: 1

ianaldo21
ianaldo21

Reputation: 669

You will always have 1 in the list, because you are creating a new instance of ListG with new List(); So when this is passed back to the controller, it will be newed up and you will add your new G object resulting in a count of 1. Alternatively you could try this: (assuming your db instance is locally accessible)

[HttpPost]
public ActionResult Save(FlowViewModel model)
{
    if (ModelState.IsValid)
    {
        Gamme G = new Gamme();
        G.ID_Gamme = model.SelectedProfile_Ga;
        G.ID_Poste = model.SelectedPoste;

        G.Next_Posts = model.PosteSuivantSelected;
        G.Nbr_Passage = int.Parse(model.Nbr_Passage);
        G.Position = int.Parse(model.Position);
        model.ListG = db.Gammes.ToList();
        model.ListG.Add(G);
        db.SaveChanges();
    }
    return RedirectToAction("Index");
}

Upvotes: 1

Related Questions