0

i have used @Html.ActionLink for moving to one form. I want to check some condition before moving to that page. condition like If user have access rights for that (Edit) page then only he can redirect to Edit page.

I'm trying it using jquery but it is only redirection to the edit page by default without calling jquery event .

How to do this ?

Priyanka
  • 2,802
  • 14
  • 55
  • 88

3 Answers3

0

Using Jquery

 <a href="@Url.Action("NewLaunches", "Updates")" style="cursor:pointer" id="deleteLink">Delete</a>

$(document).ready(function(){
  $("#deleteLink").click(function(event){
    event.preventDefault();
    bool isallowed= // Logic for finding access permission
    if(isallowed)
    {
    var url = $(this).attr("href"); // picking up the url
    document.location.href=url;
    }
    });});
amesh
  • 1,311
  • 3
  • 21
  • 51
  • 1
    Checking access rights and giving permission on client side is not a recommended approach. – amesh Oct 26 '12 at 07:21
0

Yes, you can do that.

You said you are using @Html.ActionLink for moving between forms. So I assume one of your ActionLink may look like this....

@Html.ActionLink("Edit","Home")

and your Controller may look like this...

public class HomeController : Controller
{
    public ActionResult Edit()
    {        
        return View();
    }

}

here inside the Edit action you can check for the required access right, for example :

public class HomeController : Controller
{
    public ActionResult Edit()
    {       
        if(Request.IsAuthenticated && SomeOtherCondition()) {
        {
            // if all ok, then forward to Edit page
            return View();
        }
        else{
            // send back to home.
            return("Index");
        }
    }

}

P.S : I highly recommend you do to the access rights validation on the server side and not on the client side (jquery).

Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
0

That is server logic, not client, although if you use some client security algorithms with secret keys, uid's and etc, with active communication with server by ajax, it will give you some advantages. I would recommend you: return RedirectToAction, throw exceptions, return 403 status code, check permissions on server and do not render this links and etc. @amesh example will allow user to navigate with disabled javascript in browser.

webdeveloper
  • 17,174
  • 3
  • 48
  • 47