我正在使用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>
答案 0 :(得分:0)
这里有很多问题:
我会这样做:
实施例: 查看:强>
<%
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="submit" value="Appliquer" id="appliquer" />
</div>
<强>控制器强>
public ActionResult appl(string SelectedProfile_Ga, FlowViewModel model)
{
//SelectedProfile_Ga must match the NAME of your dropdownlist, if it does, it will contain the selected value
}
此解决方案可能需要您进行一些修改,因为我无法知道您的数据类型,并且我猜到了您的下拉列表的名称。