控制器未被选中,也未返回第二个视图Razor

本文关键字:返回 第二个 Razor 视图 控制器 | 更新日期: 2023-09-27 18:27:31

我有一个下拉列表,用户在其中选择soemthing,然后它应该将所选选项发送到我的控制器并将其保存在viewbag上,然后我的控制器返回一个新视图,在新视图中,我的表体应该使用viewbag项目来表示我的模型中的语句,但由于某种原因,当用户选择该选项时,什么都不会发生。。控制器不会触发新视图或传递选定的选项这是我的控制器

namespace Arpon.Web.Brain.Pos.Controllers
{
    public class HomePosController : BaseController
    {
        //
        // GET: /HomePos/
        public ActionResult Index()
        {
            var db = new ArponClientPosContext();
            return View("~/Views/HomePos/Index.cshtml", db.Pos.ToList());
           
        }
        public ActionResult SimpleInterest(String pdvlist)
        {
            var db = new ArponClientPosContext();
            SimpleInterestModel model = new SimpleInterestModel();
            ViewBag.key1 = "0";
                ViewBag.key1 = pdvlist;
                return View("~/Views/HomePos/IndexF.cshtml", db.Pos.ToList());
        }
    }
}

还有我的观点:

@using Arpon.Web.Loader.Utils.Helpers;
@model ICollection<Arpon.Web.Brain.Pos.Models.Pos>
@ViewBag.key1
<script>
    onDocExtendedLoad(
    function run() {
        var PdvId = document.getElementById("pdvlist").value;
            $('select[name=pdvlist]').change(function () { alert($(this).val()); });
    }
    );
</script>
<div class="page-title">
    <i class="fa fa-bar-chart-o"></i>
    <h3>POS</h3>
</div>
<div class="row">
    <div class="col-md-12">
        <div class="row">
            <div class="col-md-12 sortable">
                <div class="grid simple vertical green">
                    <div class="grid-title no-border">
                        <h4>@this.FP("Punto de Venta")</h4>
                        <div class="tools">
                            <a href="javascript:;" class="collapse"></a>
                            <a href="javascript:;" class="reload"></a>
                        </div>
                    </div>
                    <div class="grid-body no-border">
                        <div class="p-l-20 p-r-20 p-b-10 p-t-10 b-b b-grey">
                            <div class="row">
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <h5 class="bold pull-left m-r-5">@this.FP("lbl.loader.date")</h5>
                                        <div class="controls">
                                            <div class="input-append success no-padding">
                                                <div id="reportrange" class="pull-right div-daterangepicker">
                                                    <i class="fa fa-calendar fa-lg"></i>
                                                    <span>@date.ToString("d MMMM, yyyy") - @date.ToString("d MMMM, yyyy")</span> <b class="caret"></b>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <h5 class="bold pull-left m-r-5">@this.FP("Restaurante")</h5>
                                        <div class="controls">
                                            <div class="input-append success no-padding">
                                                <select class="ctl-source">
                                                    <option value="">Todos</option>
                                                    <option value="DE">Desayuno</option>
                                                    <option value="CO">Comida</option>
                                                    <option value="CE">Cena</option>
                                              
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <h5 class="bold pull-left m-r-5">@this.FP("PDV")</h5>
                                        <div class="controls">
                                            <div class="input-append success no-padding">
                                                @using (Html.BeginForm())
                                                {
                                                    <select id="pdvlist" name="pdvlist" class="ctl-source" onchange="run()">
                                                        <option value=" ">Todos</option>
                                                        @foreach (var item in Model.Select(l => l.Pdv).Distinct())
                                                        {
                                                            <option value="@item">@item</option>
                                                        }
                                                    </select>
                                                    <input type="submit" value="Actualizar" />
                                                }
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row" id="dateWidgetIncomeBanquetPanel">
                            
                                <table class="table table-hover no-more-tables table-iconmebanquet-detail">
                                    <thead>
                                        <tr>
                                            <th style="">Platillo</th>
                                            <th style="">PV</th>
                                            <th style="">1</th>
                                            <th style="">2</th>
                                            <th style="">3</th>
                                            <th style="">4</th>
                                            <th style="">5</th>
                                            <th style="">6</th>
                                            <th style="">7</th>
                                            <th style="">8</th>
                                            <th style="">9</th>
                                            <th style="">10</th>
                                            <th style="">11</th>
                                            <th style="">12</th>
                                            <th style="">13</th>
                                            <th style="">14</th>
                                            <th style="">15</th>
                                            <th style="">16</th>
                                            <th style="">17</th>
                                            <th style="">18</th>
                                            <th style="">19</th>
                                            <th style="">20</th>
                                            <th style="">21</th>
                                            <th style="">22</th>
                                            <th style="">23</th>
                                            <th style="">24</th>
                                            <th style="">25</th>
                                            <th style="">26</th>
                                            <th style="">27</th>
                                            <th style="">28</th>
                                            <th>Total</th>
                                            <th>Venta</th>
                                        </tr>
                                    </thead>
                                   
                                    <tbody>
                                        @foreach (var item in Model)
                                        {
                                            var total = 0;
                                            decimal costo = 0;
                                            for (int i = 1; i <= 28; i++)
                                            {
                                                var value = 0;
                                                if (item.Fecha.Day == i) { value = item.Cantidad; costo = costo + item.Total; }
                                                total += value;
                                            }
                                            <tr>
                                                <td>@item.Descripcion</td>
                                                <td>@((costo / total).ToString("C"))</td>
                                                @for (int i = 1; i <= 28; i++)
                                                {
                                                    var value = 0;
                                                    int month = item.Fecha.Month;
                                                    if (item.Fecha.Day == i) { value = item.Cantidad; }
                                                    <td>
                                                        <a href="javascript:void(0)" data-toggle="popover"
                                                           data-html="true" data-original-title="@i/@month/2015" data-placement="bottom" data-content="Producto: @item.Descripcion<br />PV:@((costo / total).ToString("C"))<br />Total:@value<br />Venta:@(((costo / total) * value).ToString("C"))" data-trigger="hover">@value</a>
                                                    </td>
                                                }
                                                <td>@total</td>
                                                <td>@(((costo / total) * total).ToString("C"))</td>
                                            </tr>
                                        }
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
   
                           
</div>

我做错了什么?任何帮助都将被视为

控制器未被选中,也未返回第二个视图Razor

重载Html.BeginForm()将呈现一个表单标记,其中action属性值设置为当前action方法。这意味着,如果您从HomePos控制器的Index操作执行此代码,Razor将生成以下标记

<form action="/Home/Index" method="post">   
  <!-- Other form elements-->
</form>

既然你想把它发布到不同的操作方法,你可以尝试这个过载

public static MvcForm BeginForm(
    this HtmlHelper htmlHelper, string actionName,  string controllerName
)

将您的视图代码更新为

@using (Html.BeginForm("SimpleInterest","HomePos"))
{
  <select id="pdvlist" name="pdvlist" class="ctl-source">
      <option value="No">No</option> 
  </select>
  <input type="submit" value="Actualizar" />
}

现在在您的代码中,我不知道什么是onDocExtendedLoad。但看起来您正试图在更改事件上执行run方法。在run方法中,每次都要向下拉列表中注册一个更改事件处理程序。我不确定这是你真正想做的事情。如果你只是想在用户更改下拉列表时获得所选的选项值,你可以使用一些不引人注目的javascript来做到这一点。

@section Scripts
{
  <script>
    $(function(){  
       $('select[name=pdvlist]').change(function (e) {
          var selectedVal = $(this).val();
          alert(selectedVal);
          // If you want to submit the form
          // $(this).closest("form").submit();
       });
    });
</script>
}