2012-01-12 123 views
0

我有一個表與misc。數據。每一行都有一個複選框。此表格不在表格中。我想用jQuery從每個「checked行」中獲取一個值,將這些值添加到一個表單中,然後通過傳統POST發佈到我的控制器方法中。通過jQuery添加表單值,並將POST添加到控制器方法

我搶的價值觀,我需要這樣的:

$('#some-button').click(function(){ 

    var theValues = []; 

    $('#some-table tbody tr td:first-child input:checkbox').each(function(){ 
     if(this.checked){ 
      theValues.push($(this).parent().next().val()); 
     } 
    }); 
    $('#some-form').val(theValues.serialize()).submit(); 
}); 

視圖中的形式如下:

@using(Html.BeginForm("TheAction", "TheController", 
    FormMethod.Post, new { @id = "some-form" })) 
{ 
    <input id="some-button" type="button" value="Do Stuff" /> 
} 

我的控制方法是這樣的:

[HttpPost] 
public ActionResult TheAction(IEnumerable<string> values) 
{ 
    // Do stuff with values... 

    return RedirectToAction("SomewhereElse"); 
} 

問題是,沒有任何反應。毫不誇張地說,Firebug正在展示零活動。根據在FB控制檯中運行jQuery語句(我從表格行中獲取所需的值)正在抓取值。

任何想法???

UPDATE:

按普利文的建議,我已經使用隱藏的表單字段嘗試,但問題依然存在,

將此添加到形式:

@Html.Hidden("values", new { @id = "the-values" }) 

並更新了我的JavaScript到這裏:

$('#some-button').click(function(){ 

    var theValues = []; 

    $('#some-table tbody td td:first-child input:checkbox').each(function(){ 
     if(this.checked){ 
      theValues.push($(this).parent().next().val()) 
     }   
    }); 
    $('#the-values').val(theValues); 
    $('#some-form').submit(); 
}); 

再次,問題依然存在。 JS的'click'事件正在觸發,但沒有發生POST。沒什麼,納達,拉鍊。

回答

1

試着改變你的形式是這樣的:

@using(Html.BeginForm("TheAction", "TheController", FormMethod.Post, new { @id = "some-form" })) 
{ 
    <input id="some-button" type="button" value="Do Stuff" /> 
} 
+0

我的代碼確實有FormMethod.Post,只是忘了把它放在我的問題。 – Didaxis 2012-01-12 15:57:39

+0

在你的控制器中放置一個斷點;它會受到打擊嗎? – 2012-01-12 15:58:15

+0

未碰到 – Didaxis 2012-01-12 18:32:17

0

上述方法的工作原理,是有史以來值要訪問你可以把它們放在隱藏的變量。

@using(Html.BeginForm("TheAction", "TheController", FormMethod.Post, new { @id = "some-form" })) {  <input id="some-button" type="button" value="Do Stuff" /> 
    @Html.HiddenFor................ 
} 
+0

請參閱我上面的編輯 – Didaxis 2012-01-12 16:38:30

相關問題