C# 从视图中检索值并将其保存在ASP MVC 3的列表中

C# 从视图中检索值并将其保存在ASP MVC 3的列表中,c#,sql-server,visual-studio-2010,asp.net-mvc-3,C#,Sql Server,Visual Studio 2010,Asp.net Mvc 3,我正在使用C#和SQLServer2005开发一个ASP.NETMVC3应用程序 我还使用实体框架和代码优先方法 我想将视图中选定或输入的值(以表单形式)保存在局部变量中 因此,我在视图模型中创建一个列表: public List<Gamme> ListG = new List<Gamme>(); 这是输入值的视图: <% using (Html.BeginForm("Save", "Anouar")) { %> <%: Html.Validation

我正在使用C#和SQLServer2005开发一个ASP.NETMVC3应用程序

我还使用实体框架和代码优先方法

我想将视图中选定或输入的值(以表单形式)保存在局部变量中

因此,我在视图模型中创建一个列表:

public List<Gamme> ListG = new List<Gamme>();
这是输入值的视图:

<% using (Html.BeginForm("Save", "Anouar")) { %>
<%: Html.ValidationSummary(true) %>
<fieldset class="parametrage">
    <legend>Gestion de Gamme</legend>

    <div><%:Html.Label("Poste :")%><%: Html.DropDownList("SelectedPoste", Model.PostesItems)%><input type="checkbox" name="option1" value="Poste Initial" id= "chkMain" onclick="test();"/>Poste Initial<input type="checkbox" name="option2" value="Poste Final" id= "chkFirst" onclick="test2();"/>Poste Final</div>


     <div><%:Html.Label("Nombre de Passage :")%><%: Html.EditorFor(x=>x.YourGammeModel.Nbr_Passage)%></div>
    <div><%:Html.Label("Position :")%><%: Html.EditorFor(x=>x.YourGammeModel.Position)%></div>
    <div><%:Html.Label("Poste Précédent :")%><%: Html.DropDownList("PostePrecedentSelected", Model.PostesItems)%></div>
    <div><%:Html.Label("Poste Suivant :")%><%: Html.DropDownList("PosteSuivantSelected", Model.PostesItems)%></div>


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

    </fieldset>

在您的控制器中,您可以放置一个静态字典,而不是保存在数据库中。。。并在不再需要时使用类似“Singleton”模式将其清除:


@在你看来,你必须有一个表格,将邮件发送给你的控制员,我将使用BeginForm??而UserGammes在控制器中是未知的?尝试使用控制器生成(Ctrl+M,Ctrl+C)从模型Gamme生成控制器和视图,然后可以将UserGammes对象放入此控制器中否,,,我不能!因为它们绑定到另一个视图和控制器请检查我的更新,,,我按照您的指示操作
<% using (Html.BeginForm("Save", "Anouar")) { %>
<%: Html.ValidationSummary(true) %>
<fieldset class="parametrage">
    <legend>Gestion de Gamme</legend>

    <div><%:Html.Label("Poste :")%><%: Html.DropDownList("SelectedPoste", Model.PostesItems)%><input type="checkbox" name="option1" value="Poste Initial" id= "chkMain" onclick="test();"/>Poste Initial<input type="checkbox" name="option2" value="Poste Final" id= "chkFirst" onclick="test2();"/>Poste Final</div>


     <div><%:Html.Label("Nombre de Passage :")%><%: Html.EditorFor(x=>x.YourGammeModel.Nbr_Passage)%></div>
    <div><%:Html.Label("Position :")%><%: Html.EditorFor(x=>x.YourGammeModel.Position)%></div>
    <div><%:Html.Label("Poste Précédent :")%><%: Html.DropDownList("PostePrecedentSelected", Model.PostesItems)%></div>
    <div><%:Html.Label("Poste Suivant :")%><%: Html.DropDownList("PosteSuivantSelected", Model.PostesItems)%></div>


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

    </fieldset>
private static Dictionary<string, Gamme> userGammes;

        public static Dictionary<string, Gamme> UserGammes
        {
            get
            {
                if (userGammes == null)
                {
                    userGammes = new Dictionary<string, Gamme>();
                }
                return userGammes;
            }
        }
public ActionResult Save(Gamme gamme)
         {
             UserGammes.Add("currentUserID", gamme);

         }
private static Dictionary<string, Gamme> userGammes;

public static Dictionary<string, Gamme> UserGammes
{
    get 
    {
       if (userGammes== null)
       {
          userGammes = new Dictionary<string, Gamme>();
       }
       return userGammes;
    }
}
public ActionResult Save(Gamme gamme)
{
    UserGammes.Add("currentUserID", gamme);
    // ... do stuff
}