我如何在选项标签asp.net mvc 5中使用html.actionlink或ajax.action

时间:2015-09-21 16:11:49

标签: asp.net asp.net-mvc asp.net-ajax

我的Actionlink无效请帮助

  <div data-role="select">
        <select id="js-data-example-ajax" name="js-data-example-ajax">
  @foreach (var item in Model)
  {
   <option value=" 3620194">
   @Html.ActionLink(@item.ProductName, "GetByID", new { Area = "Common", ID = item.ProductID })
   </option>
  }
</select>
</div>

OR

   <div data-role="select">
   <select id="js-data-example-ajax" name="js-data-example-ajax">
 @foreach (var item in Model)
  {
   <option value=" 3620194">
   @Ajax.ActionLink(@item.ProductName, "GetByID", new AjaxOptions { HttpMethod = "Get", UpdateTargetId = "js-data-example-ajax", InsertionMode = InsertionMode.ReplaceWith })
   </option>
  }
</select>
</div>

在Home Controller中,动作链接不在GetByID方法

    public ViewResult GetByID(string ID)
    {
       return View();
    }

1 个答案:

答案 0 :(得分:1)

option元素内的链接不会呈现为链接。相反,你需要自己处理点击,例如使用jQuery。你可以这样做:

HTML

<div data-role="select">
    <select id="js-data-example-ajax" name="js-data-example-ajax">
        @foreach (var item in Model)
        {
            <option value="3620194" data-url="@Url.Action("GetByID", new { Area = "Common", ID = item.ProductID })">
                @item.ProductName
            </option>
        }
    </select>
</div>

的jQuery

<script type="text/javascript">
    $(function () {
        $('#js-data-example-ajax').on('change', function() {
            var url = $('#js-data-example-ajax').find(":selected").data('url');
            document.location = url;
        });
    });
</script>