【问题标题】:How do I dynamically add an Actionlink from the Controller in MVC 4如何在 MVC 4 中从控制器动态添加 Actionlink
【发布时间】:2013-07-26 23:16:52
【问题描述】:
   [HttpGet]
    public JsonResult EfficientPaging(int? page = null, string searchParam = null)
    {
        int skip = page.HasValue ? page.Value - 1 : 0;

        _users = db.Users.Select(u => u).ToList();

        var data = _users.Skip(skip * 10).Take(10).ToList();
        var empty = _users.Any();
        var count = Convert.ToInt32(Math.Ceiling(_users.Count() / 10.0));

        if (!string.IsNullOrEmpty(searchParam))
        {
            empty = !_users.Any(u => u.last_name.ToLower().Contains(searchParam.ToLower()));
            count = _users.Count(u => u.last_name.ToLower().Contains(searchParam.ToLower()));
            data =
                _users.Where(u => u.last_name.ToLower().Contains(searchParam.ToLower()))
                  .Skip(skip * 10)
                  .Take(10)
                  .ToList();
        }


        var grid = new WebGrid(data);
        var htmlString = grid.GetHtml(tableStyle: "webGrid",
                                      headerStyle: "header",
                                      alternatingRowStyle: "alt",
                                      htmlAttributes: new {id = "DataTable"},
                                      columns: grid.Columns(
                                          grid.Column("first_name", "First"),
                                          grid.Column("last_name", "Last"),
                                          grid.Column("username", "Username"),
                                          grid.Column("password_expiration", "Expired"),
                                          grid.Column("locked", "Locked"),
                                          grid.Column(format: (item) => Html.Actionlink("Edit", "Edit", "User", new { userId = item.id }, null))));




        return Json(new
        {
            Data = htmlString.ToHtmlString(),
            Count = count,
            Empty = empty
        }, JsonRequestBehavior.AllowGet);
    }

JQuery:

$(document).ready(function () {
    $('input').keyup(function () {
        var name = $(this).attr("name");
        var searchParameter = $(this).val();
        var element = $(this);
        $.ajax({
            type: "GET",
            url: "User/EfficientPaging",
            data: { searchParam: searchParameter },
            dataType: "json",
            success: function (d) {
                element.parent().parent().find(".file").empty();
                if (d.Empty) {
                    element.parent().parent().find(".file").append("<span style='color: black' >No files in this folder.</span>");
                    element.parent().parent().find(".pagination").empty();
                    element.parent().parent().find(".pagination").hide();
                } else {
                    // add the dynamic WebGrid to the body
                    element.parent().parent().find(".file").append(d.Data);
                    element.parent().parent().find(".pagination").empty();
                    element.parent().parent().find(".pagination").show();
                    element.parent().parent().find(".pagination").append(paging(1, d.Count, 5));
                    pagingEvent();
                }
            }
        });
        //$("#uploadFile").show();
        //$("#uploadButton").show();
        return false;
    });
});

我正在尝试创建一个异步搜索框,除了编辑链接之外,它运行良好。 Html.Actionlink 抛出错误,“当前上下文中不存在名称‘Html’。”

VS 建议使用 System.Web.Mvc.Html 添加,但 System.Web.Mvc.Html.ActionLink 不存在。

【问题讨论】:

    标签: c#-4.0 asp.net-mvc-4 webgrid actionlink


    【解决方案1】:

    你真的不应该在控制器中创建 html。

    话虽如此,请尝试

    grid.Column(format: (item) => 
    string.Format("<a href='{0}'>{1}</a>", Url.Action("Edit", "User", new { userId = item.id }), "Edit")
    )))
    

    与此相同

    grid.Column(format: (item) => 
    {
    var link = Url.Action("Edit", "User", new { userId = item.id });
     return   string.Format("<a href='{0}'>{1}</a>", link, "Edit")
    
    })))
    

    也和

    一样
    grid.Column(format: (item) => GetLink(item.Id))))
    
    /* later */
    
    private string GetLink(int id){
         return string.Format("<a href='{0}'>{1}</a>", Url.Action("Edit", "User", new { userId = id }), "Edit");
    }
    

    【讨论】:

    • 我不确定如何传递 ID。
    • 非常感谢您的帮助。上面的代码发生了最奇怪的事情。后面的代码是: Edit 但屏幕上的“a”标签是字面上打印在屏幕上,没有编辑链接。
    • 我在想,因为浏览器没有重新加载,编辑链接是不可能的。
    • 我在原帖中添加了它们
    • @kyle 如果它在后面的代码上而不是在那里,那就太奇怪了。您可能在某处缺少括号或其他东西
    【解决方案2】:

    我必须按以下格式添加 ActionLink:

    grid.Column(format: (item) => new HtmlString(string.Format("<a href='{0}'>{1}</a>", Url.Action("Edit", "User", new {userId = item.id}), "Edit"))
                                                  )));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多