C# 如何在ASP MVC中将表单中输入的值保存到列表中
我正在使用C#和SQLServer2005开发一个ASP.NETMVC3应用程序 我还使用实体框架和代码优先方法 我想从“Gestion.ascx”视图中检索值,并使用按钮将它们保存在列表中 这是我尝试的,但没有任何结果:C# 如何在ASP MVC中将表单中输入的值保存到列表中,c#,sql-server,asp.net-mvc-3,C#,Sql Server,Asp.net Mvc 3,我正在使用C#和SQLServer2005开发一个ASP.NETMVC3应用程序 我还使用实体框架和代码优先方法 我想从“Gestion.ascx”视图中检索值,并使用按钮将它们保存在列表中 这是我尝试的,但没有任何结果: The View Gestion : <% using (Html.BeginForm("Save", "Anouar")) { %> <%: Html.ValidationSummary(true) %> <fieldset c
The View Gestion :
<% 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>
最后是模型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 Gamme YourGammeModel { get; set; }
public int SelectedProfile_Ga { get; set; }
public int SelectedGamme{ get; set; }
public int SelectedPoste { get; set; }
public int PostePrecedentSelected { get; set; }
public int PosteSuivantSelected { get; set; }
private static Dictionary<string, Gamme> userGammes;
public static Dictionary<string, Gamme> UserGammes
{
get
{
if (userGammes == null)
{
userGammes = new Dictionary<string, Gamme>();
}
return userGammes;
}
}
}
公共类FlowViewModel
{
[关键]
公共字符串IDv{get;set;}
[未映射]
public SelectList PostesItems{get;set;}
公共列表配置文件_GaItems{get;set;}
公共列表GaItems{get;set;}
公共Gamme YourGammeModel{get;set;}
public int SelectedProfile_Ga{get;set;}
public int SelectedGamme{get;set;}
public int SelectedPoste{get;set;}
public int postePresentSelected{get;set;}
公共int PosteSuivantSelected{get;set;}
私有静态字典用户域;
公共静态字典用户域
{
得到
{
if(userGammes==null)
{
userGammes=新字典();
}
返回用户域;
}
}
}
在控制器中,是否检查了ModelState
。它有效吗。您可以使用ModelState.IsValid
进行检查。如果无效,则表示模型中存在问题binding@saravanan控制器并不像您看到的那样完整。实际上,UserGammes.Add(“currentUserID”,gamme);它的下划线是红色的。有什么建议吗?没有问题,您可以尝试使用return View()
进行调试,并了解发生了什么。@saravanan lol好的,但我如何知道保存成功?我以为您报告了无法进行模型绑定。
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 Gamme YourGammeModel { get; set; }
public int SelectedProfile_Ga { get; set; }
public int SelectedGamme{ get; set; }
public int SelectedPoste { get; set; }
public int PostePrecedentSelected { get; set; }
public int PosteSuivantSelected { get; set; }
private static Dictionary<string, Gamme> userGammes;
public static Dictionary<string, Gamme> UserGammes
{
get
{
if (userGammes == null)
{
userGammes = new Dictionary<string, Gamme>();
}
return userGammes;
}
}
}