DropDownListFor在更改时不会调用函数

时间:2016-05-18 20:58:20

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

我是html,razor和MVC的新手,所以如果这是一个简单的解决方案,我很抱歉,但我已经解决了这个问题一段时间没有解决方案。我希望触发一个控制器操作,每次下拉列表更改时重新填充部分视图。我已尝试使用'onchange'html属性实现此功能,并通过设置下拉列表的id并使用jquery函数触发它。但我无法获得以下三个功能中的任何一个。这是我的代码..

@model AislesOnlineDataControl.Models.PickupPointsListModel

@{
  ViewBag.Title = "PickupPointsList";
}

<script>
   function Select() {
     @Html.Action("PickupPartial", "PickUpPoint", new { mod = Model.points[Model.selectedID] });
   };

  $("#PupDropDown").on('change', function () {
    @Html.Action("PickupPartial", "PickUpPoint", new { mod = Model.points[Model.selectedID] });
  });

   $(function() {
     $('#PupDropDown').change(function () {
        Model.selectedID = Convert.ToInt32(selectPoint.SelectedValue);
        //window.location.reload();
        @Html.Action("PickupPartial", "PickUpPoint", new { mod = Model.points[Model.selectedID] });
     });
   });
</script>

<h2>Manage Pick-Up Points</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

     <div class="form-horizontal">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        @{SelectList selectPoint = new SelectList(Model.selectedPoint, "Value", "Text");}
       <div class="form-inline">
          <div class="row">
             @Html.DropDownListFor(model => model.selectedID, selectPoint, null, new { @id="PupDropDown", @onchange = "Select()" })
             @Html.ActionLink("New", "CreatePickup", "PickUpPoint", null, new { @class = "btn btn-default pull-right" })
          </div>
       </div>
       <div>
          @Html.Partial("~/Views/PickUpPoint/PickupPartial.cshtml", Model.points[Model.selectedID])
       </div>
    </div>
}

1 个答案:

答案 0 :(得分:0)

@Html.Action(..)从您的局部视图返回ActionResult,最终证明是一个html代码块。所以你不能把你的HTML代码放在JavaScript块中。

您需要的是JavaScript代码,以便为操作和渲染操作结果ajax call

以下是在asp.Net MVC

中进行简单ajax调用的示例

https://stackoverflow.com/a/16186212/2802809