2010-05-03 11 views
2

MVC 페이지에서 이와 같이 생성되는 동적 폼을 만들고 싶습니다.동적 폼 및 AntiForgeryToken MVC

onclick=" 
    var f = document.createElement('form'); 
    f.style.display = 'none'; 
    this.parentNode.appendChild(f); 
    f.method = 'POST'; 
    f.action = this.href; 
    var s = document.createElement('input'); 
    s.setAttribute('type', 'hidden'); 
    s.setAttribute('name', 'authenticity_token'); 
    s.setAttribute('value', '6I6td2wJRI9Nu5Au/F3EfOQhxJbEMXabuVXM0nXonkY='); 
    f.appendChild(s); 
    f.submit(); 
    return false;" 

위와 같이 AntiForgeryToken을 어떻게 구현할 수 있는지 잘 모르겠습니까?!? 어떤 helpwould이

답변

2

을 이해할 수있을 당신이 사실이다 동적 양식 대신 GET의 게시 할 수 있도록 양식에 앵커 링크를 변환하는 노력을 호출하는 것으로 보인다.

그래서 대신 :

이 경우 당신이 나중에 onclick 이벤트에서 모든 자바 스크립트를 사용하여 형태로 변신 것 대신에 링크를 방출로 귀찮게의 서버에 직접 양식을 생성하는 당신을 추천 할 것입니다
<%= Html.ActionLink("OK", "controller", "action", null, 
    new { onclick = "Some ugly javascript" })%> 

직접 수 :

<% using (Html.BeginForm("controller", "action")) { %> 
    <%= Html.AntiForgeryToken() %> 
    <input type="submit" value="OK" /> 
<% } %>