0

已经为此苦苦挣扎了一天多,我如何将 actionlink id 传递给 jquery ajax 调用以启用部分页面更新。

<li>@Html.ActionLink(@item.strCountry, "Index", "Weather", new { id = Regex.Replace(@item.strCountry, " ", "-") }, new { @class = "getCities" })</li>

jQuery

<script>
 $(function () {
     $(".getCities").click(function () {
         $.ajax({
             //url: this.href,
             url: '@Url.Action("pvDisplayListOfWeatherCities", "Weather")',
             type: 'GET',
             data: { id: $('#id').val() },
             dataType: 'Json',
             success: function (result) {
                 alert(result.test);
             },
             error: function () {
                 alert("error");
             }
         });
         return false;
     });
 });
</script>

谢谢

乔治

4

3 回答 3

1

将参数添加为 HTML 属性

<li>@Html.ActionLink(@item.strCountry, "Index", "Weather", 
    new { id = Regex.Replace(@item.strCountry, " ", "-") }, 
    new { @class = "getCities", data_param1 = Regex.Replace(@item.strCountry, " ", "-") })</li>

这将呈现:

<li><a class="getCities" href="/Weather/Index/val" data-param1="val">country</a></li>

然后使用jQuery.attr()方法:

<script>
 $(function () {
     $(".getCities").click(function () {
         $.ajax({
             //url: this.href,
             url: '@Url.Action("pvDisplayListOfWeatherCities", "Weather")',
             type: 'GET',
             data: { id: $(this).attr("data-param1") }, // <-- param1 etc.
             dataType: 'json',
             success: function (result) {
                 alert(result.test);
             },
             error: function () {
                 alert("error");
             }
         });
         return false;
     });
 });
</script>
于 2013-02-05T10:48:13.103 回答
1

如果我正确理解了这个问题,那么你会改变:

data: { id: $('#id').val() },

data: { id: $(this).attr('id') },

编辑- 你所需要的只是data: { id: $(this).text() },

于 2013-02-05T10:40:08.303 回答
1

您没有id为此链接设置属性,您只是设置用作参数来构建链接本身的 id。您应该简单地id为此标签添加属性

<li>@Html.ActionLink(@item.strCountry, "Index", "Weather", 
    new { id = Regex.Replace(@item.strCountry, " ", "-") }, 
    new { @class = "getCities", @id = Regex.Replace(@item.strCountry, " ", "-")})</li>

如果您希望它与链接中的参数相同id。然后更新这个

data: { id: $('#id').val() },

对此

data: { id: $(this).attr('id') },

获取id此 HTML 标记的实际属性。

于 2013-02-05T11:05:14.960 回答