Asp.net mvc 如何捕获选定的行ID-MVC表
我在MVC中有表格布局(见下面的代码),在每个表格行上都有一个提交按钮。每个提交按钮都发送到相同的控制器方法“TableSample”。如何捕获所选行id并发布它Asp.net mvc 如何捕获选定的行ID-MVC表,asp.net-mvc,asp.net-mvc-4,razor,Asp.net Mvc,Asp.net Mvc 4,Razor,我在MVC中有表格布局(见下面的代码),在每个表格行上都有一个提交按钮。每个提交按钮都发送到相同的控制器方法“TableSample”。如何捕获所选行id并发布它 public class TableSample { public string Property1 { get; set; } public string Property2 { get; set; } public int Property3 { get; set; } public List
public class TableSample
{
public string Property1 { get; set; }
public string Property2 { get; set; }
public int Property3 { get; set; }
public List<Things> Things;
}
@Html.TextBoxFor(m => m.Property1)
@Html.TextBoxFor(m => m.Property2)
@Html.TextBoxFor(m => m.Property3)
<table>
<tbody>
@foreach (var thing in Model.Things)
{
<tr>
<td>@thing.ID</td>
<td>@thing.Name</td>
<td><input type="submit" value="Select" name="Command" /></td>
</tr>
}
</tbody>
</table>
[HttpPost]
public ActionResult TableSample(TableSample sample, string Command)
{
if (Command == "Select")
{
//How to capture selected row ID?
}
if (Command == "Other")
{
}
}
公共类表示例
{
公共字符串属性1{get;set;}
公共字符串属性2{get;set;}
公共int属性3{get;set;}
公开物品清单;
}
@Html.TextBoxFor(m=>m.Property1)
@Html.TextBoxFor(m=>m.Property2)
@Html.TextBoxFor(m=>m.Property3)
@foreach(Model.Things中的var thing)
{
@东西ID
@东西,名字
}
[HttpPost]
public ActionResult TableSample(TableSample示例,string命令)
{
如果(命令==“选择”)
{
//如何捕获选定的行ID?
}
如果(命令==“其他”)
{
}
}
使用javascript捕获提交按钮,单击并将行id放置在隐藏字段中,这样它将与其他字段一起提交
如果行id不是模型的一部分,您可以简单地向action方法添加一个与隐藏字段同名的参数
如果你需要更多的细节,请告诉我。我在我的一个mvc应用程序中做了基本相同的事情
基本上有三个步骤:
1) 添加隐藏的输入。我们将只使用直接的HTML,而不使用帮助程序,因为该字段不会是模型的一部分。将其放置在表单中的某个位置:
<input type="hidden" id="rowId" name="rowId" />
3) 添加javascript以捕获submit按钮,单击并将行id放置在隐藏字段中。我更喜欢jQuery,我假设您可以访问它,因为它是MVC4的标准:
$(function () {
$('input[name="command"]').click(function () {
// because there is a command button on each row it is important to
// retrieve the id that is in the same row as the button
$('#rowId').val($(this).parents('tr:first').children('td:first').html());
});
});
使用javascript捕获submit按钮,单击并将行id放置在一个隐藏字段中,这样它将与其余字段一起提交 如果行id不是模型的一部分,您可以简单地向action方法添加一个与隐藏字段同名的参数 如果你需要更多的细节,请告诉我。我在我的一个mvc应用程序中做了基本相同的事情 基本上有三个步骤: 1) 添加隐藏的输入。我们将只使用直接的HTML,而不使用帮助程序,因为该字段不会是模型的一部分。将其放置在表单中的某个位置:
<input type="hidden" id="rowId" name="rowId" />
3) 添加javascript以捕获submit按钮,单击并将行id放置在隐藏字段中。我更喜欢jQuery,我假设您可以访问它,因为它是MVC4的标准:
$(function () {
$('input[name="command"]').click(function () {
// because there is a command button on each row it is important to
// retrieve the id that is in the same row as the button
$('#rowId').val($(this).parents('tr:first').children('td:first').html());
});
});
如果您注意到rowID的含义,这将有点容易,因为您的代码中没有rowID。但据我所知,你指的是第一排的身份证 在控制器中:
[HttpPost]
public ActionResult TableSample(TableSample sample, string Command, int rowid)
{
if (Command == "Select")
{
rowid
}
if (Command == "Other")
{
}
}
鉴于:
<script>
$('input[name=Command]').click(function(){
var rowID = $(this).closest('tr').find(".rowid").val()
$post('/Home/TableSample?rowid='+rowID+ '&Command=Select')
});
</script>
<table>
<tbody>
@foreach (var thing in Model.Things)
{
<tr>
<td class="rowid">@thing.ID</td>
<td>@thing.Name</td>
<td><input type="submit" value="Select" name="Command" /></td>
</tr>
}
</tbody>
</table>
$('input[name=Command]')。单击(函数(){
var rowID=$(this.closest('tr').find(.rowID”).val()
$post('/Home/TableSample?rowid='+rowid+'&Command=Select')
});
@foreach(Model.Things中的var thing)
{
@东西ID
@东西,名字
}
如果您注意到rowID的含义,那就有点容易了,因为您的代码中没有rowID。但据我所知,你指的是第一排的身份证
在控制器中:
[HttpPost]
public ActionResult TableSample(TableSample sample, string Command, int rowid)
{
if (Command == "Select")
{
rowid
}
if (Command == "Other")
{
}
}
鉴于:
<script>
$('input[name=Command]').click(function(){
var rowID = $(this).closest('tr').find(".rowid").val()
$post('/Home/TableSample?rowid='+rowID+ '&Command=Select')
});
</script>
<table>
<tbody>
@foreach (var thing in Model.Things)
{
<tr>
<td class="rowid">@thing.ID</td>
<td>@thing.Name</td>
<td><input type="submit" value="Select" name="Command" /></td>
</tr>
}
</tbody>
</table>
$('input[name=Command]')。单击(函数(){
var rowID=$(this.closest('tr').find(.rowID”).val()
$post('/Home/TableSample?rowid='+rowid+'&Command=Select')
});
@foreach(Model.Things中的var thing)
{
@东西ID
@东西,名字
}
谢谢。当然让我先试试。谢谢。当然让我先试试。