2012-04-16 45 views
0

我使用jQuery的tempate來建立我的表。當用戶點擊我的表格中的一個單元格時,我在此單元格上添加屬性「名稱」。我如何才能將單元格td導入控制器?

我想檢索哪個單元格被點擊,並與哪個值,但我不成功,以檢索我的價值細胞到控制器asp mvc。 我使用FormCollection,但我沒有找到我的單元格TD,我也試着request.form [「cellTd」]但它不工作。 在我的控制器中,cell等於null,而form不包含元素cellTd。

感謝您的幫助。

我的觀點:

@using (Html.BeginForm("Action", "Test", FormMethod.Post)) 
{ 
<div id="TableToFill"></div> 
<button id="send" value="send" name="btn" type="submit">send</button> 
} 

我的模板:

<script id="TableTemplate" type="text/x-jquery-tmpl"> 
<table class="Table"> 
<tbody class="BodyTable"> 
    {{each list}} 
    <tr class="Item">  
     <td description="Standard${Name}" >${this.Value}</td> 
     {{if $index == 0}} 
     <td ><input description="high${Name}" value="" type="text" /></td> 
     {{/if}} 
    </tr>  
    {{/each}} 
</tbody> 
</table>  
</script> 

我的代碼的jquery

<script type="javascript"> 
$.getJSON("getTable", params, function (items) { 
     $("#TableTemplate").tmpl(items).appendTo("#TableToFill");   
    }); 
    $(".Table tbody .Item td").click(function(){ 
    $(this).attr("name","cellTd"); 
    }); 
</script> 

我控制器

public class TestController : controller 
{ 
    public ActionResult(FormCollection form) 
    { 
     String cell=Request.Form["cellTd"]; 
    } 
} 
+0

@Ropstah,我必須多表與multiselected在我的控制器發送。用簡單的輸入發送信息是不可能的?否則,我創建一個功能,發送所有具有名稱的元素... – Zoners 2012-04-16 14:25:12

+0

您希望在控制器中獲得什麼值? – Ropstah 2012-04-16 16:01:49

+0

@Ropstah我想收到所有填充了屬性名稱的元素。我有不同的價值觀到我的單元格,我有一個簡單的文本(字符串或整數)或一個應答器img(我添加一個屬性'說明'在舞臺上img知道圖像的價值) – Zoners 2012-04-17 08:31:50

回答

0

你應該把你的請求click事件td內:

$(".Table tbody .Item td").click(function(){ 
    //you are setting the name to "CellTd" 
    $(this).attr("name", "cellTd"); 

    var tdNameValue = $(this).attr("name"); //will always give "cellTd" as it is set above... 

    //make your ajax request here 
    $.ajax({ 
     url: 'urltocontroller', 
     data: {tdName : tdNameValue}, 
     dataType: 'json', 
     success: function(r) { 
      //successcallback 
     }, 
     type: 'POST' 
    }); 
}); 

現在,你可以在這裏找回你的價值:

public class TestController : controller 
{ 
    public ActionResult(FormCollection form) 
    { 
     String cell=Request.Form["cellTd"]; 
    } 
} 
相關問題