C# 在ASP MVC和EF中,列表的值传递NULL
我正在使用C#和SQLServer2005开发一个ASP.NETMVC3应用程序 我还使用实体框架和代码优先方法 我有一些参数值,我想把它们保存在一个列表中 我已经这样做了,但现在的问题是,当我下次储蓄时,新的价值观会打破旧的价值观 接下来,我使用F10一步一步地调试项目,发现列表在执行结束时变为空 这是控制器“ProfileGaController”的代码:C# 在ASP MVC和EF中,列表的值传递NULL,c#,visual-studio-2010,asp.net-mvc-3,C#,Visual Studio 2010,Asp.net Mvc 3,我正在使用C#和SQLServer2005开发一个ASP.NETMVC3应用程序 我还使用实体框架和代码优先方法 我有一些参数值,我想把它们保存在一个列表中 我已经这样做了,但现在的问题是,当我下次储蓄时,新的价值观会打破旧的价值观 接下来,我使用F10一步一步地调试项目,发现列表在执行结束时变为空 这是控制器“ProfileGaController”的代码: namespace MvcApplication2.Controllers { public class ProfileGaC
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>();
}
公共类FlowViewModel
{
[关键]
公共字符串IDv{get;set;}
[未映射]
public SelectList PostesItems{get;set;}
公共列表配置文件_GaItems{get;set;}
公共列表GaItems{get;set;}
公共字符串SelectedProfile_Ga{get;set;}
公共字符串SelectedPoste{get;set;}
公共字符串PostePresentSelected{get;set;}
公共字符串PosteSuivantSelected{get;set;}
公共字符串位置{get;set;}
公共字符串Nbr_通道{get;set;}
public List ListG=新列表();
}
这是部分视图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>
格姆手势
model.SelectedPoste,model.posteItems)%%>
型号:Nbr_通道)%>
型号:Nbr_通道)%>
型号(位置)%>
model.PosteSuivantSelected,model.posteItems,新建{@id=“dd”})%>
哪里是空值。你也能分享这些代码吗?在执行的最后,等一下,我会把controller@Shyju你喜欢吗我把它放在Git上了