【问题标题】:How can i set data json to Ajax.actionLink?如何将数据 json 设置为 Ajax.actionLink?
【发布时间】:2015-03-14 15:07:09
【问题描述】:

如标题。如何将数据 json 设置为 Ajax.actionLink? Ajax 方法返回对话框内的所有用户。我想为每一行创建 actionlink 并在控制器中调用我的方法 RemoveUserByid。这是我的代码:

$("#remove").click(function () {
    $('.choiceOption').dialog("close");
    $(".dialog").html('');
    $(".dialog").dialog({ //Shows dialog
        height: 300,
        width: 450,
        modal: true,
        resizable: false,
        title: "Usuń uzytkownika:"
    });
    //
    $.ajax({
        url: '/User/GetUsers/',
        type: 'POST',
        contentType: "application/json",
        success: function (json) {      
            $(".dialog").html('');
            for (var i = 0; i < json.length; i++) {
                var rowText = "<tr style='border-bottom: 1px dotted rgb(195, 178, 178); display: block;'>
    <td style='padding-right: 5px'>" + json[i].userid+ "</td>
    <td style='padding-right: 5px'>" + json[i].Name + "</td>
    <td style='padding-right: 5px'>" + json[i].date + "</td>
    <td style='padding-right: 5px;float:right;'>
       //how to create a link that takes me to the controller using the json data?
  '@Ajax.ActionLink(" ", "RemoveUserByid", "User", new { user= json[i].id //syntax error !!!},new AjaxOptions { UpdateTargetId = "users"})';
    </td></tr>";$(".dialog").append(rowText);}
            if (json == 0) {
                $(".dialog").append("Brak ");
            }

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("Wystąpił problem z przesłaniem danych, spróbuj ponownie za chwile: " + errorThrown)
        }
    });


});

我将不胜感激。 问候!

【问题讨论】:

    标签: c# jquery ajax json asp.net-mvc


    【解决方案1】:

    json变量属于javascript作用域,不能被c#编译器引用。 您可以使用假常量发出默认链接文本,并在浏览器端将其替换为真实用户 ID,如下所示:

     $.ajax({
        url: '/User/GetUsers/',
        type: 'POST',
        contentType: "application/json",
        success: function (json) {      
            $(".dialog").html('');
            var pattern = '@Ajax.ActionLink(" ", "RemoveUserByid", "User", new { user= "FAKEID" },new AjaxOptions { UpdateTargetId = "users"})';
          for (var i = 0; i < json.length; i++) {
         var rowText = "<tr style='border-bottom: 1px dotted rgb(195, 178, 178); display: block;'><td style='padding-right: 5px'>"
                        + json[i].userid + "</td><td style='padding-right: 5px'>" + json[i].Name +
          "</td><td style='padding-right: 5px'>" + json[i].date + "</td>" + 
          "<td style='padding-right: 5px;float:right;'>" + pattern.replace("FAKEID", json[i].id) + "</td></tr>";
                $(".dialog").append(rowText);}
            if (json == 0) {
                $(".dialog").append("Brak ");
            }
    
        }, }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-23
      • 2020-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-17
      相关资源
      最近更新 更多