0

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

我也在使用实体框架和代码优先方法。

我有一些参数值,我想将它们保存在列表中。

我已经这样做了,但是现在的问题是,当我下次保存时,新值会压碎旧值。

因此,我使用 F10 逐步调试项目,发现列表在执行结束时变为 NULL。

这是控制器“ProfileGaController”的代码:

namespace MvcApplication2.Controllers
{ 
    public class ProfileGaController : Controller
    {
        private GammeContext db = new GammeContext();

        public ActionResult Gestion(FlowViewModel model)
        {
            return PartialView(model);
        }

        [HttpGet]
        public ActionResult Index(Profile_Ga profile_ga, Poste poste)
        {             
            var viewModel = new FlowViewModel();
            viewModel.PostesItems = new SelectList(db.Postes.ToList(), "ID_Poste", "ID_Poste");            
            viewModel.Profile_GaItems = db.Profil_Gas.ToList();
            viewModel.GaItems = db.Gammes.ToList();

            return View(viewModel);
        }

        public ViewResult Details(string id)
        {
            Profile_Ga profile_ga = db.Profil_Gas.Find(id);
            return View(profile_ga);
        }

        public ActionResult Create()
        {            
            return View();
        } 

        [HttpPost]
        public ActionResult Create(Profile_Ga profile_ga)
        {
            if (ModelState.IsValid)
            {
                db.Profil_Gas.Add(profile_ga);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(profile_ga);
        }

        public ActionResult Edit(string id)
        {
            Profile_Ga profile_ga = db.Profil_Gas.Find(id);
            return View(profile_ga);
        }

        [HttpPost]
        public ActionResult Edit(Profile_Ga profile_ga)
        {
            if (ModelState.IsValid)
            {
                db.Entry(profile_ga).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(profile_ga);
        }

        public ActionResult Delete(string id)
        {
            Profile_Ga profile_ga = db.Profil_Gas.Find(id);
            return View(profile_ga);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {            
            Profile_Ga profile_ga = db.Profil_Gas.Find(id);
            db.Profil_Gas.Remove(profile_ga);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        [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");           
        }
    }
}

这是视图模型“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>(); 
}

这是部分视图 Gestion.ascx 的代码:

<%@  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>
4

0 回答 0