We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Hide/Show Editable links in webGrid CSHTML code

I am trying to Hide/Show the 'Edit' link in each row of a given Grid. Basically only few user roles have the access to Modify the information. I wrote a method ActionResult in controller class to check the user access and returning Boolean value. Based on True/False I need to Hide/Show the 'Edit' Links in a grid. The design code is written in MVC CSHTML. Please suggest how it can be achieved. I tried using JQuery but I can hide the Edit column only for the first row of the grid. Below is the code in CSHTML and Jquery.


CSHTML

 <table border="1" cellpadding="0" cellspacing="1" style="width: 90%;"><tr> <td> @grid.GetHtml(tableStyle: "webGridMedium", headerStyle: "header", alternatingRowStyle: "alt", selectedRowStyle: "select", columns: grid.Columns( grid.Column("BeginDate", "Begin Date", style: "description"), grid.Column("Status", "Status", style: "description"), grid.Column("", style: "description10", format: @<a class="edit-status" id="btnEditStatus" rel='nofollow' href="">Edit</a>) )) </td> </tr> </table>

JQuery Code: The parameter data is boolean value I get it from Controller class.

<script> $(document).ready(function () { checkAccess(); } function checkAccess() { // debugger; $.ajax({ url: '/Employee/CheckAccess', type: 'POST', data: {}, success: function (data) { if (data == false) { document.getElementById("btnEditStatus").style.visibility = "hidden"; } else if (data = true) { document.getElementById("btnEditStatus").style.visibility = ""; } }, error: (function (result) { alert("Failed access! Please contact administrator."); }) }) } </script>

5 Replies

MS Mani Sankar Durai Syncfusion Team August 4, 2016 10:33 AM UTC

Hi nag, 

We have analyzed your query and found the cause of the issue. Since you have rendered the Edit link to be visible or hide by using document.getElementById("btnEditStatus") in the ajax post. So it is the cause of the issue. By using that it will traverse only one time if the returned Boolean value is either true or false. In case of that we achieved your requirement by using refreshTemplate event of Syncfusion grid. It will traverse each cell and makes it to either visible or hide according to Boolean value. So we suggest you to traverse each cell of edit link instead of giving document.getElementById("btnEditStatus”) 
 
For your convenience please refer the below code example, 
 
<script type="text/x-jsrender" id="columnTemplate"> 
 
    <a class="edit-status" id="btnEditStatus" rel='nofollow' href="">Edit</a> 
         
</script> 
 
@(Html.EJ().Grid<object>("Editing") 
 
        .Columns(col => 
        { 
            … 
            col.HeaderText("Button").Template("#columnTemplate").Width(100).Add(); 
        }) 
            .ClientSideEvents(eve => eve.TemplateRefresh("refreshTemplate")) 
) 
<script> 
     
    function refreshTemplate(args) { 
        var cell = args; 
        $.ajax({ 
            url: '/Home/CheckAccess', 
            type: 'POST', 
            data: {}, 
            success: function (data) { 
                if (data == "False") { 
                    cell.cell.style.visibility = "hidden"; 
                } 
                else if (data = "True") { 
                    cell.cell.style.visibility = ""; 
                } 
            }, 
            error: (function (result) { 
                alert("Failed access! Please contact administrator."); 
            }) 
        }) 
    } 
 
</script> 

 
We have also prepared a sample based on Syncfusion Grid that can be available from the below link, 
 
If you still face the issue please get back to us with the following detais, 
1.       Is that you using Syncfusion Grid or webGrid in MVC? 
2.       Send the code that you rendered in controller page? 
 
The provided information will help us to analyze the issue and provide you the response as early as possible, 
 
Regards, 
Manisankar Durai. 
 
 



NA nag August 4, 2016 02:04 PM UTC

Thank you for the responding. I am using Webgrid in MVC and I can't find ClientSideEvents when I use regular MVC webgrid.

Here is my method in controller class

public ActionResult CheckAccess()

{

try

{

bool CanUserHasEditAccess = false;

string USER_ID = "UYUI45"

CanUserHasEditAccess = "TRUE";

return Json(CanUserHasEditAccess);

}

}














































































}

catch

{

throw;

}

}



MS Mani Sankar Durai Syncfusion Team August 5, 2016 11:22 AM UTC

Hi Nag, 

We have suspected that you are using Syncfusion Grid. So, we have updated the solution in the previous response for Syncfusion Grid. Since you asked the support for WebGrid in MVC which is provided by Microsoft. 
Please refer the following link, 
 
Regards, 
Manisankar Durai. 



NA nag August 5, 2016 03:13 PM UTC

Thank you so much :). Wonderful. It's worked.


MS Mani Sankar Durai Syncfusion Team August 8, 2016 10:56 AM UTC

Hi Nag,  
Thanks for your feedback. 
 
We are happy to hear that your requirement is achieved.  
 
Regards, 
Manisankar Durai. 
                                                                                                                  


Loader.
Live Chat Icon For mobile
Up arrow icon