【问题标题】:Value passed NULL between View and Controller in ASP MVC在 ASP MVC 中的视图和控制器之间传递 NULL 的值
【发布时间】:2013-05-31 14:10:43
【问题描述】:

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

【问题讨论】:

  • 您的数据应该在模型中,而不是会话中。
  • @JeremyHolovacs 好的,但我如何在ActionResult appl 中使用SelectedProfile_Ga 的值?
  • 在按钮单击事件上使用 jquery 侦听器,并收集下拉值。然后在重定向中发送这个值。
  • @JeremyHolovacs thx,,,但我真的是一个初学者,我从来没有写过 jquery 代码,,,你有什么想法吗?
  • 有很多关于 jQuery 的教程,如果你想做一个中规中矩的 MVC 项目,你需要对它有一定的了解。我建议你从那里开始。

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


【解决方案1】:

这里有很多问题:

  1. Url.Action("Application", "ProfileGa") 正在导航到 /Application 不是 /appl
  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
}

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

【讨论】:

  • 感谢答案,但是当我更改按钮定义时,我点击的那一刻,它会点击动作appl 而不是显示页面(HttpGet)的动作Application 和这将产生此错误:未找到视图“appl”或其主视图,或者没有视图引擎支持搜索的位置。搜索了以下位置:
  • 我不确定 ProfileGa 是什么,但如果这不是您的控制器的名称,我会在 BeginForm() 中删除它。另外,您的问题可能不清楚,您想采用哪种观点?我假设您正在尝试重定向到 /appl。另请注意,我删除了 [httpPost] 属性,因为鉴于您所拥有的,我认为不需要该属性。
  • 是的,ProfileGa 是我的控制器但是 Application 是一个显示视图的操作(正如您在其代码中看到的,它包含我用来在下拉列表中加载值的列表)但是,操作 @987654325 @ 用于在我单击视图中的按钮时将值记录在我的基础中
  • 根据您的编辑进行了更新。 appl 之前是否被调用过,只是没有使用选定的值?您必须确保所选值有要绑定的内容,因此要么将其添加到您的模型中,要么按照我展示的操作并添加一个新参数以将其绑定到。请记住,无论您将其放在何处,它都必须与输入字段具有完全相同的名称。
  • 之前是否调用过 appl,只是没有使用所选值? ==> appl 仅在点击 id="appl" 的按钮时被调用,,,我看到代码中没有修改?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多