我正在使用 C# 和 SQL Server 2005 开发一个 ASP .Net MVC 3 应用程序。
在一个视图中,我有一个下拉列表和一个按钮。
该按钮用于将我带到另一个视图。
我想将下拉列表中所选项目的值传递给控制器,因为它将用于另一个操作。
所以我尝试使用它来做到这一点,Session
但该值仍然通过 NULL。
这是视图:
<% using (Html.BeginForm("appl", "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="Appliquer" id="appliquer" onclick="location.href='<%: Url.Action("Application", "ProfileGa") %>'" />
</div>
这是控制器:
[HttpGet]
public ActionResult Application()
{
var vv = new FlowViewModel();
vv.FaItems = new SelectList(db.Familles.ToList(), "ID_Famille", "ID_Famille");
vv.SFItems = new SelectList(db.Sous_Familles.ToList(), "ID_SFamaille", "ID_SFamaille");
vv.PItems = new SelectList(db.Produits.ToList(), "Code_Produit", "Code_Produit");
vv.FFF = db.Familles.ToList();
vv.SSS = db.Sous_Familles.ToList();
vv.PPP = db.Produits.ToList();
vv.NSItems = db.Ns_AFaires.ToList();
this.Session["ggg"] = vv.SelectedProfile_Ga;
return View(vv);
}
[HttpPost]
public ActionResult appl(FlowViewModel model)
{
Famille fam = new Famille();
fam = db.Familles.Find(model.SelectedFamille);
fam.ID_Gamme = model.SelectedProfile_Ga;
db.Entry(fam).State = EntityState.Modified;
db.SaveChanges();
return View(model);
}
笔记 :
在下拉列表中选择的项目是SelectedProfile_Ga
。
该操作app
是当我单击视图中的按钮时执行的操作,Application
并且在此操作中我要检索所选项目的值。
编辑
查看应用程序:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication2.Models.FlowViewModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Application
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Application</h2>
<% using (Html.BeginForm("appl", "ProfileGa")) { %>
<body onload="charge()">
<fieldset><legend>Veuillez choisir le type :</legend>
<div>
<input type="submit" value="Appliquer" id="appl" />
</div>
</fieldset>
<form id="form1" >
<h2><%= Html.Encode(ViewData["Message"]) %> </h2>
<div>
<%:Html.Label("Famille :")%><%: Html.DropDownListFor(model => model.SelectedFamille, Model.FaItems, new { @id = "ff" })%>
</div>
<div>
<%:Html.Label("Sous Famille :")%><%: Html.DropDownListFor(model => model.SelectedSFamille, Model.SFItems, new { @id = "ss" })%>
</div>
<div>
<%:Html.Label("Produit :")%><%: Html.DropDownListFor(model => model.SelectedPdt, Model.PItems, new { @id = "pp" })%>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Num_Serie)%>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.Num_Serie, new { @id = "nn" })%>
</div>
</form>