0

我使用@Html.ActionLink 移动到一种形式。在转到该页面之前,我想检查一些情况。条件如如果用户对该(编辑)页面具有访问权限,那么只有他可以重定向到编辑页面。

我正在使用 jquery 尝试它,但默认情况下它只是重定向到编辑页面而不调用 jquery event 。

这个怎么做 ?

4

3 回答 3

0

使用 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;
    }
    });});
于 2012-10-26T07:03:09.050 回答
0

是的,你可以这么做。

您说您@Html.ActionLink用于在表单之间移动。所以我假设你的一个 ActionLink 可能看起来像这样......

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

你的控制器可能看起来像这样......

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

}

在 Edit 操作中,您可以检查所需的访问权限,例如:

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");
        }
    }

}

PS:我强烈建议您在服务器端而不是客户端(jquery)上进行访问权限验证。

于 2012-10-26T07:04:48.213 回答
0

那是服务器逻辑,而不是客户端,尽管如果您使用一些带有密钥、uid 等的客户端安全算法,并通过 ajax 与服务器进行主动通信,它会给您带来一些优势。我建议你:返回RedirectToAction、抛出异常、返回 403 状态码、检查服务器上的权限并且不呈现此链接等。@amesh 示例将允许用户在浏览器中使用禁用的 javascript 进行导航。

于 2012-10-26T07:07:22.017 回答