ASP MVC中View和Controller之间的值传递NULL

时间:2013-05-31 14:10:44

标签: c# sql-server visual-studio-2010 asp.net-mvc-3 entity-framework

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

1 个答案:

答案 0 :(得分:0)

这里有很多问题:

  1. Url.Action(“应用程序”,“ProfileGa”)正在导航到/ Application 不是/ appl
  2. 按钮中的javascript不会导致回发,因此您永远不会在控制器中获取值
  3. 您应该考虑切换到razor语法
  4. 即使你有神奇的工作,你也只是发送了一个id 但是你试图绑定到FlowViewModel,为什么不只是期望 您发送的单一值?
  5. 我会这样做:

    1. 你有一张表格,使用它
    2. 改变您的控制器只能期望您实际传递的内容 翻过
    3. 实施例: 查看:

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

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