1

我正在使用 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>
4

1 回答 1

0

你在这里有很多问题:

  1. Url.Action("Application", "ProfileGa") 正在导航到 /Application 而不是 /appl
  2. 按钮中的 javascript 不会导致回发,因此您永远不会在控制器中获得值
  3. 您应该认真考虑切换到剃刀语法
  4. 即使你已经神奇地工作了,你只是发送一个 id 但你试图绑定到 FlowViewModel,为什么不只期待你发送的单个值呢?

我会这样做:

  1. 你有一个表格,使用它
  2. 更改您的控制器以仅期望您实际传递的内容

示例: 查看:

<% 
   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
}

此解决方案可能需要您进行一些修改,因为我不可能知道您的数据类型,并且我猜到了您的下拉列表的名称。

于 2013-05-31T14:48:11.557 回答