2012-09-10 1 views
1
@Ajax.ActionLink("like", "Like", "Article", new { postId = Model.post.PostId, userName = User.Identity.Name }, new AjaxOptions { OnBegin = "OnBegin" }, new { @class = "like_link" }) 

function OnBegin() 
{ 
    if(true) // value from client side. not returning value from server. 
    { 
     // I dont want to work the link. I want only alert message. 
    } 
    else 
    { 
     // Go to controller with parameters. 
    } 
} 

위와 같은 것을 원합니다. OnBegin은 그것을 할 필요가 없습니다. 또 다른 해결책이 될 수 있습니다.유효성 검사가 거짓이면 Ajax.ActionLink가 작동하지 않습니까?

감사합니다.

답변

2

OnBegin해야합니다.

function OnBegin() { 
    if (true) // value from client side. not returning value from server. 
    { 
     // I dont want to work the link. I want only alert message. 
     alert('alerting some message'); 
     return false; 
    } 
    else { 
     // Go to controller with parameters. 
     return true; 
    } 
} 
+0

나는 ActionLinks에 AjaxOptions 물건 중 하나를 해본 적이 없다 : 간단하게 true 또는 false 당신이 컨트롤러 액션 여부를 실행할지 여부에 따라 반환합니다. 그것이 구문이라면, 모든 이벤트 와이어 업 코드가 당신을 위해 수행되기 때문에, 내 방식보다 훨씬 쉽습니다. – Gromer

+0

false를 반환하고 true로하면 충분합니다. 좋습니다. 고마워. –

0

양식이 유효하지 않은 경우 사용자가 링크의 URL로 이동하지 못하도록하려면이 내용을 읽는 중입니다. 이 경우 링크의 클릭 이벤트에 연결하고 javascript로 유효성 검사를 수행 한 다음 모든 것이 유효하면 사용자를 진행 시키거나 양식이 유효하지 않으면 중지 할 수 있습니다. jQuery의 경우 다음과 같습니다.

$(document).ready(function() { 
    $('a.like_link').click(function (e) { 
     if (formIsNotValid) { // Insert a real conditional here that works for your needs. 
      e.preventDefault(); 

      // Display a message here? 
     } 

     // Don't need an else, everything is valid, let the user move on. 
    }); 
}); 

편집 : 사용자가 자바 스크립트를 사용하지 않으면 링크가 작동합니다. 기회가 있습니다, 귀하의 유효성 검사는 그 경우에는 작동하지 않을 수도 있습니다.

관련 문제