【问题标题】:Controller not getting selected nor returning second view Razor控制器没有被选中也没有返回第二个视图 Razor
【发布时间】:2015-12-18 21:35:32
【问题描述】:

我有一个下拉列表,用户选择某些东西,然后它应该将选定的选项发送到我的控制器并将其保存在 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>

我做错了什么? 任何帮助将不胜感激

【问题讨论】:

  • 您将帖子返回到 Index(), not SimpleInterest(). If you want to post to SimpleInterest()` 然后您需要在 BeginForm() 方法中指定操作名称。 javascript Run() 方法的目的是什么?
  • 我添加了 run 方法,因为我选择了执行 run() onchange 的选项,但也没有工作,我如何在 beginForm() 中指定操作?
  • @using (Html.BeginForm("SimpleInterest", yourControllerName, FormMethod.Post)) { ...
  • 只是"HomePos"(不是Arpon.Web.Brain.Pos.Controllers.HomePosController

标签: c# asp.net-mvc-4 razor controller


【解决方案1】:

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

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

既然你想发布到不同的action方法,你可以试试这个overload

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 是什么。但看起来你正试图在 change 事件上执行 run 方法。在 run 方法中,您正在向下拉列表 everytime 注册一个更改事件处理程序。我不确定这是你真正想做的事情。如果您只是想在用户更改下拉菜单时获取选定的选项值,则可以使用一些不显眼的 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>
}

【讨论】:

    猜你喜欢
    • 2016-09-25
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多