2012-04-05 64 views
1

我有窗體,其中我有文本框的值和鏈接(添加)。當用戶將數據輸入到文本框並單擊添加時,它應該將輸入的文本添加到textArea的同一視圖中。我試圖用ajax.actionlink做這個,但我似乎無法弄清楚如何將輸入的文本傳遞給參數。任何人都可以有另一個想法,我可以嘗試..我不能使用Ajax.beginForm,因爲我有HTML表單已經。MVC2.0閱讀文本框,並在同一視圖的文本區域添加值

預先感謝您。

這裏是html代碼,我到目前爲止與ajax操作鏈接。

    <div class="editor-label"> 
         <%: Html.Label("DefectID/FeatureID")%> <%: Html.TextBox("DefectID", "", new { @class = "required" })%> <%: Ajax.ActionLink("Add", "InsertDefectIDs", new AjaxOptions { UpdateTargetId = "DefectIds" }) %><%--<input type="button" name="add" value="Add" />--%> 
        </div> 

        <div class="editor-field"> 
         <%: Html.TextAreaFor(model => model.DefectIds, new { rows = 5, cols = 12, @readonly = "true" })%> 
         <%: Html.ValidationMessageFor(model => model.DefectIds)%> 
        </div> 
+0

是否當用戶點擊添加時,如果您的最終目標是填充textarea字段,用戶輸入需要發送到服務器?您可以在客戶端使用JavaScript更輕鬆地完成此操作。請幫助我們瞭解您要完成的任務。 – jessegavin 2012-04-05 21:19:16

+0

我試圖用JavaScript做這件事,但沒有成功。是的..我的最終目標是填充文本區域字段。我不需要去服務器。 – AJP 2012-04-05 21:24:14

回答

0

您應該能像下面這樣:

HTML

<div class="editor-label"> 
    <%: Html.Label("DefectID/FeatureID")%> 
    <%: Html.TextBox("DefectID", "", new { @class = "required" })%> 
    <input type="button" name="add" value="Add" onclick="AddDefectId();" /> 
</div> 

<div class="editor-field"> 
    <%: Html.TextAreaFor(model => model.DefectIds, new { rows = 5, cols = 12, @readonly = "true" })%> 
    <%: Html.ValidationMessageFor(model => model.DefectIds)%> 
</div> 

的Javascript

<script type="text/javascript"> 
    function AddDefectId() { 
     var defectId = document.getElementById('DefectId').value; 

     if(defectId != "") 
      document.getElementById('DefectIds').value += defectId + "," 

     document.getElementById('DefectId').value = "";   
    }​ 
</script> 

這是一個直接的HTML/JavaScript的例子:http://jsfiddle.net/zJpRA/6/

相關問題