Asp.net mvc 通过MVC中的ActionLink或Submit按钮传递多个值
我的视图中有一个脚手架列表类型表。在桌子上方,我还有两个复选框。当我点击Asp.net mvc 通过MVC中的ActionLink或Submit按钮传递多个值,asp.net-mvc,Asp.net Mvc,我的视图中有一个脚手架列表类型表。在桌子上方,我还有两个复选框。当我点击Edit链接时,我需要传递所选的Id以及复选框的值 我可以使用ActionLink传递主键值,如 @Html.ActionLink("Action","Controller", new { id=@item.Id }) 我可以通过将复选框值包装成Html.Beginform之类 @using(Html.BeginForm("Action","Controller",FormMethod="Post"){ <input
Edit
链接时,我需要传递所选的Id
以及复选框的值
我可以使用ActionLink
传递主键值,如
@Html.ActionLink("Action","Controller", new { id=@item.Id })
我可以通过将复选框
值包装成Html.Beginform
之类
@using(Html.BeginForm("Action","Controller",FormMethod="Post"){
<input type="checkbox" name="Check" value="1" />
<input type="checkbox" name="Check" value="2" />
-- table
<input type="Submit" value="Edit"/>
@使用(Html.BeginForm(“操作”、“控制器”、FormMethod=“Post”){
--桌子
在我的控制器中,我可以像
[HttpPost]
Public ActionResult Edit(IEnumerable<string> check)
{ }
[HttpPost]
公共操作结果编辑(IEnumerable检查)
{ }
在这里,我需要同时获取主键值
和复选框值
,我尝试了这两种方法,并且只能获取其中任何一种。有人能帮我获取这两个值吗?提前谢谢。您可以在操作参数中包含id。例如
[HttpPost]
Public ActionResult Edit(IEnumerable<string> check, int id)
{ }
您将在“检查”字符串中获得所选值
要在提交按钮上获取名称类型jquery,请单击并将其存储在隐藏字段中,然后从表单集合访问隐藏字段
var getval = $("#Check option:selected").text();
$("#hdnText").val(getval);
这是控制器代码:
[HttpPost]
Public ActionResult Edit(string check, FormCollection collection)
{
string strText = collection["hdnText"].ToString();
string strValue = check;
}
谢谢,好主意。还有其他方法吗?也许你可以使用JavaScript获取复选框和id的值,然后手动点击操作。我在编辑
按钮旁边放置了一个隐藏字段,它在所有位置都给我相同的id(第一行的值)
。我如何获得id(行)
value?
@section scripts{
<script type="text/javascript">
function clickfunc(id) {
$("#hf").val(id);
$('form').submit();
}
</script>
}
public ActionResult EditTry(string hid)
{
return View();
}
var getval = $("#Check option:selected").text();
$("#hdnText").val(getval);
[HttpPost]
Public ActionResult Edit(string check, FormCollection collection)
{
string strText = collection["hdnText"].ToString();
string strValue = check;
}