【问题标题】:ASP .NET MVC Form Submit does not trigger Controller Action Method a second time.ASP .NET MVC 表单提交不会再次触发控制器操作方法。
【发布时间】:2017-09-27 19:50:22
【问题描述】:

我有一个局部视图,其中包含一个带有多个下拉列表和一个提交按钮的表单。提交动作调用用 HttpPost 修饰的控制器动作方法。当我运行该应用程序时,页面加载正常,提交第一次完美运行。如果我导航出页面并返回该页面并尝试提交,它根本不会触发 Action 方法 - 而是使用之前的值加载页面。

我的观点

        <h4>Filters</h4>
        <b>Season         </b>
        <br />
        @Html.DropDownList("SeasonTables", ViewBag.Seasons as SelectList, "...Select Season...", new { @class = "form-control", id = "cmbSeason", style = "width:250px;" })
        <br />
        <br />
        <b>Product Group  </b>
        <br />
        @Html.DropDownList("ProductGrpTable", ViewBag.ProductGrp as SelectList, "...Select Product Grp...", new { @class = "form-control", id = "cmbProductGrp", style = "width:250px;" })
        <br />
        <br />
        <b>Delivery Group </b>
        <br />
        @Html.DropDownList("DeliveryGrpTable", ViewBag.ProductDelGrp as SelectList, "...Select Delivery Grp...", new { @class = "form-control", id = "cmbDeliveryGrp", style = "width:250px;" })
        <br />
        <br />
        <b>Division       </b>
        <br />
        @Html.DropDownList("DivisionTable", ViewBag.DivisionList as SelectList, "...Select Division...", new { @class = "form-control", id = "cmbDivision", style = "width:250px;" })
        <br />
        <br />
        <br />
        <br />
        <br />
        <p>
            <input type="submit" value="submit" />
        </p>


    </div>
</form>
</div>

我的控制器

    [HttpPost]
      public ActionResult Index(FormCollection filterData)
    {

        Session.Remove("filterData");

        LSBusinessObject.Filter filter = new LSBusinessObject.Filter();
        filter.Season = filterData["SeasonTables"];
        filter.ProductGp = filterData["ProductGrpTable"];
        filter.ProductDelGp = filterData["DeliveryGrpTable"];
        filter.Division = filterData["DivisionTable"];


        Session["filterData"] = filter;
        lsBusinessLayer.RunSSIS(filter.Season, filter.ProductGp, filter.ProductDelGp, filter.Division);
        //persist the values
        var seasonListData = from s in lsBusinessLayer.Seasons
                             orderby s.season descending
                             select new
                             {
                                 seasonname = s.season,
                                 seasonID = s.seasonID
                             };

        SelectList seasonList = new SelectList(seasonListData, "seasonname", "seasonname", filter.Season);
        ViewBag.Seasons = seasonList;

        var ProductGpListData = from pg in lsBusinessLayer.ProdGrps
                                orderby pg.Product_Group_Name
                                select new
                                {
                                    pgID = pg.Product_Group_ID,
                                    pgName = pg.Product_Group_Name
                                };
        SelectList pgList = new SelectList(ProductGpListData, "pgName", "pgName", filter.ProductGp);
        ViewBag.ProductGrp = pgList;


        var ProductDelGpListData = from pg in lsBusinessLayer.ProdDelGrps
                                   orderby pg.Product_Delivery_Group_Name
                                   select new
                                   {
                                       pgID = pg.Product_Delivery_Group_ID,
                                       pgName = pg.Product_Delivery_Group_Name
                                   };
        SelectList pgDelList = new SelectList(ProductDelGpListData, "pgName", "pgName", filter.ProductDelGp);
        ViewBag.ProductDelGrp = pgDelList;


        var DivisionListData = from pg in lsBusinessLayer.Divisions
                               orderby pg.Product_Division_Name
                               select new
                               {
                                   pgID = pg.Product_Division_ID,
                                   pgName = pg.Product_Division_Name
                               };
        SelectList divList = new SelectList(DivisionListData, "pgName", "pgName", filter.Division);
        ViewBag.DivisionList = divList;


        Session["UpdateResult"] = "";
        Session["ShowAll"] = "false";
        return RedirectToAction("Index", "LScontrol", new { filterData = filter });
    }

我不知道我做错了什么!

【问题讨论】:

    标签: javascript c# jquery asp.net-mvc


    【解决方案1】:

    我可以通过如下重写表单定义来解决这个问题:

         @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @class = "FilterForm" }))
    

    {

                <h4>Filters</h4>
                <b>Season         </b>
                <br />
                @Html.DropDownList("SeasonTables", ViewBag.Seasons as SelectList, "...Select Season...", new { @class = "form-control", id = "cmbSeason", style = "width:250px;" })
                <br />
                <br />
                <b>Product Group  </b>
                <br />
                @Html.DropDownList("ProductGrpTable", ViewBag.ProductGrp as SelectList, "...Select Product Grp...", new { @class = "form-control", id = "cmbProductGrp", style = "width:250px;" })
                <br />
                <br />
                <b>Delivery Group </b>
                <br />
                @Html.DropDownList("DeliveryGrpTable", ViewBag.ProductDelGrp as SelectList, "...Select Delivery Grp...", new { @class = "form-control", id = "cmbDeliveryGrp", style = "width:250px;" })
                <br />
                <br />
                <b>Division       </b>
                <br />
                @Html.DropDownList("DivisionTable", ViewBag.DivisionList as SelectList, "...Select Division...", new { @class = "form-control", id = "cmbDivision", style = "width:250px;" })
                <br />
                <br />
                <br />
                <br />
                <br />
                <p>
                    <input type="submit" value="submit" />
                </p>
    

    }

    【讨论】:

      【解决方案2】:

      我承认这是在黑暗中拍摄,但尝试在上面添加 [NoCache] 装饰器 [HttpPost] 在您的控制器中。

      【讨论】:

      • 嗨 - 谢谢!但我根本没有看到 NoCache 装饰器!是 OutputCache 装饰器吗?
      • 我看到你找到了 [NoCache],很抱歉它对你不起作用。诚然,这只是一件事。
      • 无论如何,谢谢。我仍在努力解决这个问题:)
      猜你喜欢
      • 2018-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多