0

これに1日以上苦労しています.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

このリンクの属性を設定するのではなく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 に答える
1

質問を正しく理解した場合は、次のように変更します。

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

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

編集- 必要なのはdata: { id: $(this).text() },

于 2013-02-05T10:40:08.303 に答える
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 に答える