Asp.net mvc 使用复选框保存表的所有tr元素,并将其发送到MVC4中的控制器
我是MVC新手,现在遇到了一个严重的问题。对我来说,非常严重 我需要加载一个带有html会议列表的视图(加载很好),我还为每一行添加了一个复选框。现在我需要保存到数据库中,只保存选中的值。但控制器在过帐时返回null。。真的卡住了,找不到解决办法。 请看下面的代码,我已经做了 模型 看法 {Asp.net mvc 使用复选框保存表的所有tr元素,并将其发送到MVC4中的控制器,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我是MVC新手,现在遇到了一个严重的问题。对我来说,非常严重 我需要加载一个带有html会议列表的视图(加载很好),我还为每一行添加了一个复选框。现在我需要保存到数据库中,只保存选中的值。但控制器在过帐时返回null。。真的卡住了,找不到解决办法。 请看下面的代码,我已经做了 模型 看法 { 行号 名称 地址 额外的 @对于(int i=0;im.amList[i]。行代码) @DisplayFor(m=>m.amList[i].person\u name) @DisplayFor(m=>m.
行号
名称
地址
额外的
@对于(int i=0;im.amList[i]。行代码)
@DisplayFor(m=>m.amList[i].person\u name)
@DisplayFor(m=>m.amList[i].个人地址)
@Html.CheckBoxFor(m=>m.amList[i]。额外检查)
}
}
控制器
[HttpPost]
public ActionResult AddRecord(List<aMeet> spm)
{
if (spm == null)
throw new ApplicationException("No Parameters passed to Create");
int? appID = 0;
return Json(appID);
}
}
[HttpPost]
公共行动结果添加记录(列表spm)
{
如果(spm==null)
抛出新的ApplicationException(“未传递要创建的参数”);
int?appID=0;
返回Json(appID);
}
}
---它在保存时触发控制器,但模型[spm]为空。
如何纠正这个问题。在这里,我需要将选中的tr元素保存到数据库中。请请求任何帮助。我尝试了很多方法,但是模型总是空的/
非常感谢假设您的
aRecs
类有一个amList
属性,该属性是AcceptedRecords
public class aRecs
{
public List<AcceptedRecords> amList { set; get; }
}
在HttpPost操作中,您的参数将是aRecs
[HttpPost]
public ActionResult AddRecord(aRecs model)
{
foreach (var item in model.amList)
{
var lineCode = item.line_Code;
var isChecked = item.extra_checked;
/// get the record using lineCode and update the record.
}
return RedirectToAction("Index");
}
朋友们,请大家,,,任何帮助您的
aRecs
课程是什么样子的?谢谢。。它起作用了。。再次感谢您抽出时间。
<table id="tbl" style ="width:100%" cellpadding="0" cellspacing="0" class="race-fields">
<thead>
<tr>
<th style="width:500px">line code</th>
<th style="width:100px">name</th>
<th style="width:500px">address</th>
<th style="width:500px">extra</th>
</tr>
</thead>
<tbody>
<tr>
@for (int i = 0; i < Model.amList.Count; i++)
{
<tr class="EvenRow">
<td>
@Html.DisplayFor(m => m.amList[i].line_Code) </td>
<td>@Html.DisplayFor(m => m.amList[i].person_name) </td>
<td>@Html.DisplayFor(m => m.amList[i].person_address) </td>
<td>
@Html.CheckBoxFor(m => m.amList[i].extra_checked) </td>
</tr>
}
</tr>
<tr>
<span>
<input type="submit" Value ="Save"/>
</span>
</tr>
</tbody>
</table>
[HttpPost]
public ActionResult AddRecord(List<aMeet> spm)
{
if (spm == null)
throw new ApplicationException("No Parameters passed to Create");
int? appID = 0;
return Json(appID);
}
}
public class aRecs
{
public List<AcceptedRecords> amList { set; get; }
}
@model YourNamespaceHere.aRecs
@using (Html.BeginForm("AddRecord","Home"))
{
<table>
@for (int i = 0; i < Model.amList.Count; i++)
{
<tr class="EvenRow">
<td>
@Html.DisplayFor(m => m.amList[i].line_Code)
</td>
<td>@Html.DisplayFor(m => m.amList[i].person_Name) </td>
<td>
@Html.CheckBoxFor(m=>m.amList[i].extra_checked)
@Html.HiddenFor(m => m.amList[i].line_Code)
</td>
</tr>
}
</table>
<input type="submit"/>
}
[HttpPost]
public ActionResult AddRecord(aRecs model)
{
foreach (var item in model.amList)
{
var lineCode = item.line_Code;
var isChecked = item.extra_checked;
/// get the record using lineCode and update the record.
}
return RedirectToAction("Index");
}