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

Kendo MVC Grid Ajax Binding - Redirect to different pages based on authentication in ClientTemplate

3 Answers 422 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Aarti
Top achievements
Rank 1
Aarti asked on 25 Mar 2014, 04:38 PM
Hi,

I have a Kendo MVC grid ajax binding. In one of the column I am displaying hyperlink using clientTemplate  , when user clicks on the hyperlink  it opens up another window with new controller action which is working fine too. But now the requirement  when user clicks on hyperlink check user session has not expired and user still authenticated and if it is so then open up that new window otherwise clicking on hyperlink should open login controller action.

 I have written a javascript function on onlcik event for <a> tag  which checks if user has authenticated then open up new window  else redirect to login page But its not working. when I run it I get the empty grid. Below is the code.
 
@(Html.Kendo().Grid<Anthology.Services.Interfaces.DTO.Web.Models.DocumentHistoryModel>()
                          .Name("Grid")
                          .EnableCustomBinding(true)
                          .BindTo(Model)
                          .Columns(columns =>
                          {
                              columns.Bound(c => c.DocumentType).Title("Document Type").Filterable(false).ClientTemplate("<a onclick=\"IsUserAuthenticated('" + "#= DocumentGuid #')\" " + " href='javascript:void(0)'>#= DocumentType#</a>");
                              columns.Bound(c => c.Action).Filterable(false);
                              columns.Bound(c => c.DocumentThumbnail).Title("Document Thumbmail").Sortable(false).Filterable(false).ClientTemplate("<text><img src='" + Url.Content("~/content/images/BDC4B60F-1EEC-4FE8-BB9C-2CA9F2F29664.png") + "' height='35' width='30' onmouseover='MagnifyImage()'/></text>");
                          })
                         .DataSource(dataSource => dataSource.Ajax()
                              .Read(read => read.Action("Index_Read", "Home"))
                              .PageSize(5)
                              .ServerOperation(true)
                          )
                          .Events(events => events.DataBound("onGridDataBound"))
                          )  

  function IsUserAuthenticated(documentguid) {
        var userAuthenticated= @User.Identity.IsAuthenticated.ToString().ToLower();
        if (userAuthenticated==true) {
            var url = @Url.Action("Index", "ImageViewer") + "/" + documentguid;
            $(".k-grid-content table tbody tr td text img").href = url;
            $(".k-grid-content table tbody tr td text img").target = '_blank';
        } else {
            var urlAd= @Url.Action("Index", "Admin");
            $(".k-grid-content table tbody tr td text img").href = urlAd;}

    }







3 Answers, 1 is accepted

Sort by
0
Aarti
Top achievements
Rank 1
answered on 25 Mar 2014, 05:41 PM
Hi ,

Can please someone answer for this? Thank you .
0
Aarti
Top achievements
Rank 1
answered on 25 Mar 2014, 10:51 PM
I got this worked . Below is the code But the problem  I am getting now is I need to click twice on anchor  tag  to display new window. On first click I don't get the page redirected to new window but get that on second click. Can you please let me know what I am missing. Thank you.
@(Html.Kendo().Grid<Anthology.Services.Interfaces.DTO.Web.Models.DocumentHistoryModel>()
                          .Name("Grid")
                          .EnableCustomBinding(true)
                          .BindTo(Model)
                          .Columns(columns =>
                          {
                              columns.Bound(c => c.ActivityDate).Title("Activity Date").Format("{0:mm/dd/yyyy hh:mm}").Filterable(false);
                              columns.Bound(c => c.RecordId).Title("Record Id").ClientTemplate("<a href='" + Url.Action("Index", "ImageViewer", new { Id = "#=RecordId#" }, null) + "'>#=RecordId#</a>");
                              columns.Bound(c => c.RecordName).Title("Record Name").Filterable(false);
                              columns.Bound(c => c.DocumentType).Title("Document Type").Filterable(false).ClientTemplate("<a onclick=\"IsUserAuthenticated(this,'#= DocumentGuid #')\" href='\\#'>#= DocumentType#</a>");
                              columns.Bound(c => c.Action).Filterable(false);
                              columns.Bound(c => c.DocumentThumbnail).Title("Document Thumbmail").Sortable(false).Filterable(false).ClientTemplate("<text><img src='" + Url.Content("~/content/images/BDC4B60F-1EEC-4FE8-BB9C-2CA9F2F29664.png") + "' height='35' width='30' onmouseover='MagnifyImage()'/></text>");
                          })
                          .Pageable(page => page.PageSizes(true).Refresh(true))
                          .Sortable()
                          .Filterable()
                          .DataSource(dataSource => dataSource.Ajax()
                              .Read(read => read.Action("Index_Read", "Home"))
                              .PageSize(5)
                              .ServerOperation(true)
                          )
                          .Events(events => events.DataBound("onGridDataBound"))
                          .Scrollable(scrollable => scrollable.Height("auto"))


                )
  function IsUserAuthenticated(thisA,documentguid) {
        $.ajax({
            url: ('@Url.Action("IsUserAuthenticated","Home")'),
            success: function (data) {
                if (data) {
                    //alert("logged in");
                    var urllink = '@Url.Action("Index","ImageViewer",new {Id="_documentguid"})';
                    urllink=urllink.replace('_documentguid',documentguid);
                    $(thisA).attr('href', urllink);
                    $(thisA).attr('target', '_blank');
                } else {
                    // alert("Not logged in");
                    $(thisA).attr('href', '@Url.Action("Login","Account")');
                }
            }
        });

    }
0
Petur Subev
Telerik team
answered on 27 Mar 2014, 12:19 PM
Hello Aarti,

The code that you shared triggers the IsUserAuthenticated function from the first click. Could you please demonstrate your case within a simple project so we can investigate the exact reason for this?

Kind Regards,
Petur Subev
Telerik
 

Build cross-platform mobile apps using Visual Studio and .NET. Register for the online webinar on 03/27/2014, 11:00AM US ET.. Seats are limited.

 
Tags
Grid
Asked by
Aarti
Top achievements
Rank 1
Answers by
Aarti
Top achievements
Rank 1
Petur Subev
Telerik team
Share this question
or