在ASP MVC中,视图和控制器之间传递的值为NULL

本文关键字:NULL 之间 控制器 MVC ASP 视图 | 更新日期: 2023-09-27 18:16:12

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

在ASP MVC中,视图和控制器之间传递的值为NULL

您这里有许多问题:

  1. Url。操作("Application", "ProfileGa")正在导航到/Application不是/:
  2. 按钮中的javascript不会引起回发,所以你永远不会在控制器中得到一个值
  3. 你应该认真考虑切换到razor语法
  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
}

这个解决方案可能需要你做一些修改,因为我不可能知道你的数据类型,我猜你的下拉列表的名字