我正在使用 C# 和 SQL Server 2005 开发一个 ASP .Net MVC 3 应用程序。
我有 2 个意见:
索引:包含一个 DropDownList 'Gamme' 和一个按钮 'Configurer'
Gestion : Parttail View 包含许多其他字段
当我单击“配置器”按钮时,部分视图“Gestion”ID 像这样打开:
当我填写这些字段时,我单击“注册者”按钮。所有值都将保存在列表“ListG”中。
在 DropDownList 'Poste' 中选择的项目将被删除。
问题 :
提交并从 DropDownList 中删除项目后,我希望“部分视图”仍然显示,我不必再次单击“配置器”按钮。
索引视图代码:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.master" Inherits="System.Web.Mvc.ViewPage<MvcApplication2.Models.FlowViewModel>" %>
<%@ Import Namespace="Helpers" %>
<% using (Html.BeginForm("Save", "ProfileGa"))
{ %>
<div><%:Html.Label("Gamme :")%><%: Html.DropDownListFor(model => model.SelectedProfile_Ga, new SelectList(Model.Profile_GaItems, "ID_Gamme", "ID_Gamme"), new { @id = "gg" })%>
<input type="button" value="Configurer" id="btnShowGestion" onclick="GamDis()"/>
<div id="divGestion">
<%: Html.Partial("Gestion", Model) %>
</div>
<% } %>
input type="button" value = "Valider" />
<script type="text/javascript">
$(document).ready(function () {
$('#btnShowGestion').click(function () { $('#divGestion').slideToggle("slow") });
});
</script>
视图“Gestion”的代码:
<legend>Gestion de Gamme</legend>
<div>
<%:Html.Label("Poste :")%><%: Html.DropDownListFor(model => model.SelectedPoste, Model.PostesItems, new { @id = "poste" })%>
<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" })%>
<input type="button" value="Ajouter" id="add" onclick="addtext()"/>
<script type="text/javascript">
$(function () {
$('#btnSave').click(function () {
$('#poste option:selected').remove();
});
});
</script>
<% } %>
控制器的一部分代码:
[HttpGet]
public ActionResult Index()
{
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();
viewModel.ListG = (List<Gamme>)System.Web.HttpContext.Current.Session["GammeList"];
string[] PostesID;
if(viewModel.ListG != null){
PostesID = viewModel.ListG.Select(item=>item.ID_Poste).ToArray();
viewModel.PostesItems = new SelectList(db.Postes.Where(item=>!PostesID.Contains(item.ID_Poste) ).ToList(), "ID_Poste", "ID_Poste");
}
return View(viewModel);
}
[HttpPost]
public ActionResult Save(FlowViewModel model)
{
Console.WriteLine("" + model.Nbr_Passage);
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);
((List<Gamme>)System.Web.HttpContext.Current.Session["GammeList"]).Add(G);
var list = ((List<Gamme>)System.Web.HttpContext.Current.Session["GammeList"]);
}
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 List<Famille> FaItems { get; set; }
public List<Sous_Famille> SFItems { get; set; }
public List<Produit> PItems { get; set; }
public List<Ns_AFaire> NSItems { get; set; }
public List<Fonction_Poste> FPItems { get; set; }
[NotMapped]
public SelectList NextPS { get; set; }
[NotMapped]
public SelectList GenreItems { get; set; }
public string SelectedProfile_Ga { get; set; }
public string SelectedPoste { get; set; }
public string SelectedFonction { get; set; }
public string PostePrecedentSelected { get; set; }
public string PosteSuivantSelected { get; set; }
public string Position { get; set; }
public string Nbr_Passage { get; set; }
public string SelectedGenre { get; set; }
[NotMapped]
public List<Gamme> ListG {get;set;}
public FlowViewModel()
{
FaItems = new List<Famille>();
SFItems = new List<Sous_Famille>();
}
public List<string> ID_Fonction { get; set; }
}
有什么想法可以使用 Session 来解决这个问题。