0

jquery のテンプレートを使用してテーブルを作成します。ユーザーがテーブルの 1 つのセルをクリックすると、このセルに属性「name」が追加されます。

クリックされたセルと値を取得したいのですが、値のセルをコントローラーasp mvcに取得できません。FormCollection を使用していますが、セル td が見つからず、request.form["cellTd"] も試しましたが、うまくいきません。私のコントローラーでは、セルはnullに等しく、フォームには要素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"];
    }
}
4

1 に答える 1

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"];
    }
}
于 2012-04-16T13:35:05.697 に答える