2010-05-03 47 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

看來你在呼喚動態表單其實是在努力轉換錨鏈接到表單中,這樣就可以POST而不是GET。在這種情況下,我會建議您直接在服務器,而不是與發射的鏈接困擾,你以後會變成用這一切的javascript在onclick事件在窗體上生成表單:

所以不是:

<%= Html.ActionLink("OK", "controller", "action", null, 
    new { onclick = "Some ugly javascript" })%> 

你可以直接:

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