C# 通过jQuery添加表单值,并发布到控制器方法
我有一张有misc的桌子。数据。每行都有一个复选框。这张桌子不在表格中。我想使用jQuery从每个“选中的行”中获取一个值,将这些值添加到表单中,然后通过传统的post发送到我的控制器方法 我通过以下方式获取所需的值:C# 通过jQuery添加表单值,并发布到控制器方法,c#,jquery,.net,asp.net-mvc-3,post,C#,Jquery,.net,Asp.net Mvc 3,Post,我有一张有misc的桌子。数据。每行都有一个复选框。这张桌子不在表格中。我想使用jQuery从每个“选中的行”中获取一个值,将这些值添加到表单中,然后通过传统的post发送到我的控制器方法 我通过以下方式获取所需的值: $('#some-button').click(function(){ var theValues = []; $('#some-table tbody tr td:first-child input:checkbox').each(function(){
$('#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");
}
@using(Html.BeginForm("TheAction", "TheController", FormMethod.Post, new { @id = "some-form" }))
{
<input id="some-button" type="button" value="Do Stuff" />
}
视图中的窗体如下所示:
$('#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");
}
@using(Html.BeginForm("TheAction", "TheController", FormMethod.Post, new { @id = "some-form" }))
{
<input id="some-button" type="button" value="Do Stuff" />
}
并将我的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。没什么,nadda,zip。试着像这样更改您的表单:
$('#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");
}
@using(Html.BeginForm("TheAction", "TheController", FormMethod.Post, new { @id = "some-form" }))
{
<input id="some-button" type="button" value="Do Stuff" />
}
@使用(Html.BeginForm(“TheAction”,“TheController”,FormMethod.Post,new{@id=“some form”}))
{
}
上述方法有效,无论您想要访问什么值,都可以将它们放入隐藏变量中
@using(Html.BeginForm("TheAction", "TheController", FormMethod.Post, new { @id = "some-form" })) { <input id="some-button" type="button" value="Do Stuff" />
@Html.HiddenFor................
}
@使用(Html.BeginForm(“TheAction”,“TheController”,FormMethod.Post,new{@id=“some form”})){
@Html.HiddenFor。。。。。。。。。。。。。。。。
}
我的代码中确实有FormMethod。Post,只是忘了把它放在我的问题中。请尝试通过删除作为输入参数的Ienumerable来更改控制器中方法的签名。查看是否调用该操作。如果没有,还要检查布线是否正确。