0
   [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;
    });
});

I'm trying to create an asynchronous search box, and it's working great with the exception of the edit link. Html.Actionlink throws the error, "The name 'Html' does not exist in the current context."

VS suggests adding using System.Web.Mvc.Html but System.Web.Mvc.Html.ActionLink doesn't exists.

kyle
  • 527
  • 6
  • 21

2 Answers2

1

You really shouldn't create html in the controller.

With that being said try

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

Which is the same as

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

})))

which is also the same as

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");
}
Kyle
  • 1,172
  • 1
  • 8
  • 23
  • I'm not sure how I would pass the Id. – kyle Jul 19 '13 at 14:25
  • I really appreciate your help. The weirdest thing is happening with the above code. The code behind is: Edit but on the screen the "a" tag is literally printed on the screen with no edit link. – kyle Jul 19 '13 at 14:59
  • I'm thinking because the browser isn't reloaded an edit link isn't possible. – kyle Jul 19 '13 at 15:01
  • I added them in my original post – kyle Jul 19 '13 at 16:36
  • @kyle that's very peculiar if it's on the code behind and not there. You may have a missng bracket or something somewhere – Kyle Jul 19 '13 at 17:57
0

I had to add the ActionLink in the following format:

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