2012-02-10 34 views
4

如何在使用Html.ActionLink時從文本框中讀取值,以便將值傳遞給操作?使用MVC時,如何調用Controller Action和Pass Text Box值?

我有下面的代碼:

<table> 
     <tr> 
      <th> 
       Consumer Key: 
      </th> 
      <td> 
       @Html.TextBox("ConsumerKey") 
      </td> 
     </tr> 
     <tr> 
      <th> 
       Consumer Secret Key: 
      </th> 
      <td>@Html.TextBox("ConsumerSecretKey") 
      </td> 
     </tr> 
     <tr> 
     <th> 
     </th> 
     <td>@Html.ActionLink("Retreive Access Tokens", "/Retrieve"</td> 
     </tr> 
    </table> 

基本上,我需要調用控制器操作,並通過文本框的值。

MVC怎麼可能?

我知道我可以使用只是一個HTML按鈕和AJAX調用該Action,但我希望會有另一種方式使用MVC控件。

回答

3

把你的代碼Html.BeginForm內(「檢索」,「推特」)塊,即呈現給瀏覽器的HTML將成爲表單標籤內封閉的,是這樣的:

<form method="POST" action="/Retrieve/Twitter"> 
    <table>...</table> 
</form> 

然後,當您單擊提交按鈕時,表單以及文本框中的所有值將發佈給您的MVC應用程序。然後MVC完成將這些表單值(使用@Html.TextBox(「ConsumerSecretKey」創建的文本框)及其值)映射到控制器操作的參數的工作。

在你的情況,大致有以下將要呈現給瀏覽器(跳轉鏈接將需要改變的類型輸入「提交」,因爲我在下面做:

<form method="POST" action="/Retrieve/Twitter"> 
    <table> 
     <tr> 
      <th> 
       Consumer Key: 
      </th> 
     <td> 
      <input id="ConsumerKey" name="ConsumerKey" type="text" value="" /> 
     </td> 
    </tr> 
    <tr> 
     <th> 
      Consumer Secret Key: 
     </th> 
     <td> 
      <input id="ConsumerSecretKey" name="ConsumerSecretKey" type="text" value="" /> 
     </td> 
    </tr> 

    <td><input type="submit" id="Retrieve" value="Retreive Access Tokens" /></td> 

    </tr> 

</table> 

</form> 


當這回發到您的應用程序輸入到文本框中的文本(呈現爲標記)將映射到你的動作方法的參數匹配他們的名字:

public ActionResult Retrieve(string consumerKey, string consumerSecretKey) 
{ 
    //action method code here 
} 


概念在這裏工作被稱爲模型綁定。請參閱Controllers and Action Methods in ASP.NET MVC Applications並向下滾動到「操作方法參數」部分以獲得概述

0

我用過,Html.BeginForm一個提交按鈕,然後奇怪的文本框的值被提交到自動服務器:

@using (Html.BeginForm("Retrieve", "Twitter")) 
    { 
    <table> 
     <tr> 
      <th> 
       Consumer Key: 
      </th> 
      <td> 
       @Html.TextBox("ConsumerKey") 
      </td> 
     </tr> 
     <tr> 
      <th> 
       Consumer Secret Key: 
      </th> 
      <td>@Html.TextBox("ConsumerSecretKey") 
      </td> 
     </tr> 
     <tr> 
      <th> 
      </th> 
      <td> 
       <input type="submit" id="Retrieve" value="Retreive Access Tokens" /> 
      </td> 
     </tr> 
    </table> 
    } 

在我的控制器動作:

public ActionResult Retrieve(string consumerKey, string consumerSecretKey) 
     { 
} 
+0

這就是MVC的工作原理。表單/瀏覽器提交發送到服務器的HTTP POST中的所有輸入。 MVC將根據各種綁定規則自動將輸入綁定到動作參數。如果遵循ASP.NET MVC開發指南,此功能將擴展到簡單類型之外。 – 2012-02-10 12:27:24

0

您可以使用FormCollection對象。通過使用您的形式提交按鈕

[HTTPPost] 
public ActionResult Retrieve(FormCollection collection) 
    { 
     string consumerKey=collection["ConsumerKey"]; 
     string consumerSecretKey=collection["ConsumerSecretKey"]; 
    } 

形式使用POST方法。

+0

你應該給[HttpPost]屬性 – Jacek 2012-11-13 13:08:56

0

實際上,您無法使用Html.ActionLink進行POST。您可以使用Ajax.ActionLink和設置AjaxOptions屬性來實現該功能。如果使用form不適合您,您可以始終實現jQuery函數,該函數攔截由Html.ActionLink(基於此幫助程序生成的ID)生成的單擊並將文本框中的值作爲參數添加。這裏的問題是,如果你不使用Ajax,你將使用GET方法提交值,這是一個很大的NO。應該使用GET來檢索值,而不是用於修改數據庫或其他後端數據存儲庫的內容。如果你打算使用Ajax,你可以這樣做:

$(document).ready(function() { 
    $("myActionLinkId").click(function() { 
     var textBoxValue = $("#myTextBoxId").val(); 

     $.post($(this).attr("href"), { id: textBoxValue }, function(result) { 
      alert("Result data: " + result); 
     }); 
    }); 
}); 
相關問題