This is a migrated thread and some comments may be shown as answers.

[Solved] Ajax Issue with Grid

2 Answers 203 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Forest
Top achievements
Rank 1
Forest asked on 20 Jun 2011, 01:26 PM
Hi all,

I've downloaded the MVC extensions from NuGet and am using the Grid with ajax.

Please look at the following declaration:
@(Html.Telerik().Grid(Model)
    .Name("Home")
    .DataKeys(dataKeys => dataKeys.Add(o => o.Id))
    .Columns
        (columns =>
        {
            columns.Bound(o => o.FirstName).Width(200).Filterable(true);
            columns.Bound(o => o.LastName).Width(200).Filterable(false);
 
            columns.Template(
             @<text>
 
                @if (item.VisitorStatus != null && item.VisitorStatus.Id == "P")
                {
                     @Html.ActionLink("Requeue", "Requeue", "Visitor", new { area="", id = item.Id }, null)
                }
                else
                {
                    @Html.ActionLink("Select", "Edit", "Visitor", new { area = "", Id = item.Id }, null)
                }
             </text>
            ).Title("Action");
        })
 
    .DataBinding(dataBinding =>
    {
        dataBinding.Ajax().Select("_FirstLook", "AjaxSearch").Enabled(true);
    })
 
 
    .NoRecordsTemplate("No records found")
    .Scrollable(scrolling => scrolling.Enabled(false))
    .Sortable(sorting => sorting.Enabled(true))
    .Pageable(paging => paging.Enabled(true))
    .Filterable(filtering => filtering.Enabled(true))
    .Groupable(grouping => grouping.Enabled(false))
    .Footer(true)
)

When the grid originally loads, everything works as planned.

However, whenever I go to a different page or press the refresh button in the grid; in fact, whenever I do anything that calls an ajax refresh, I lose my custom column completely.

I'm not sure if this is a bug or if I'm doing this in a fashion it wasn't intended for. 

For reference, here is the controller methods:
[HttpPost]
       public ActionResult Index(VisitorSearchViewModel model)
       {
 
           VisitorSearchResponse response =
               service.FindVisitorsBy
               (new VisitorSearchRequest()
               {
                   LastFourSSN = model.LastFourSSN,
                   LastName = model.LastName
               });
 
           if (response.Success)
           {
               Session["LastFourSSN"] = model.LastFourSSN;
               Session["LastName"]    = model.LastName;
 
               Session["List"] = response.Visitors.ToList();
 
               return View("VisitorList", response.Visitors.ToList());
           }
           else
           {
               // Return a not found view...
               return View();
           }
       }
 
       [GridAction]
       public ActionResult _FirstLook()
       {
           return View(new GridModel((List<VisitorViewModel>)Session["List"]));
       }

Any help is appreciated.

Thank you.


2 Answers, 1 is accepted

Sort by
0
Forest
Top achievements
Rank 1
answered on 20 Jun 2011, 08:21 PM
I've gotten closer with this issue.

Here is where I'm at:

columns.Bound(o => o.Id)
.Template(
 @<text>
 
    @if (item.VisitorStatus != null && item.VisitorStatus.Id == "P")
    {
         @Html.ActionLink("Requeue", "Requeue", "Visitor", new { area = "", id = item.Id }, null)
    }
    else
    {
        @Html.ActionLink("Select", "Edit", "Visitor", new { area = "", Id = item.Id }, null)
    }
 </text>
)
.ClientTemplate(
    "<a href=" +  Url.Content("~/Visitor/Edit/<#= Id #>") + ">Select</a>"
)

When the page originally loads, everything works great.

When I move to page two, or hit refresh, the hyperlink is created fine and all is well in that case.  My final issue with this is I cannot figure out how to change the link in the clientTemplate via the if statement logic you see on the server template.

Is it possible or am I going to have to expose a URL property into my ViewModel to display the appropriate hyperlink?

Please advise.

0
Accepted
Atanas Korchev
Telerik team
answered on 21 Jun 2011, 07:23 AM
Hello Forest,

 A possible implementation is to call a JavaScript function from your template:

.ClientTemplate("<#= template(data) #>");
 
 
<script>
function template(item) {
    if (item.VisitorStatus != null && item.VisitorStatus.Id == "P") {
         return "<a href='" + @Url.Action("action", "controller") + "'></a>";
    } else {
         return "<a href='" + @Url.Action("action2", "controller") + "'></a>";    
    }
}
</script>
Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Forest
Top achievements
Rank 1
Answers by
Forest
Top achievements
Rank 1
Atanas Korchev
Telerik team
Share this question
or