这个问题可能已经问过好几次了,但在我的情况下行不通,所以请容忍我。
我的控制器中有以下操作:
[HttpPost]
public ActionResult Edit(Organization obj)
{
if (ModelState.IsValid)
{
OrgRepo.Update(obj);
return RedirectToAction("Details");
}
else
return View();
}
public ActionResult Edit(int id)
{
return View();
}
我试图通过调用post编辑操作将数据更新到数据库中。为此,我调用编辑操作如下:
@foreach (var item in Model) {
var test = item.PartyId;
<tr id="@test">
<td class ="txt">
<input type="text"class="txt" value="@Html.DisplayFor(modelItem => item.Caption)"/>
</td>
<td class ="txt">
<input type="text"class="txt" value="@Html.DisplayFor(modelItem => item.NameInUse)"/>
</td>
<td class ="txt">
<input type="text"class="txt" value="@Html.DisplayFor(modelItem => item.Description )"/>
</td>
<td>
@using (Html.BeginForm())
{
@Html.ActionLink("Edit", "Edit", "Org", null, new { @obj = item })
}
</td>
</tr>
但是,当我单击编辑时,我得到了异常:参数字典包含一个空条目,用于非空类型'System.Int32‘的参数'id’,用于“System.Web.Mvc.ActionResult编辑(Int32)”方法在'Dwiza.Controllers.OrgController‘中。可选参数必须是引用类型、可空类型,或者声明为可选参数。参数名称:参数
我的问题:
发布于 2013-06-12 00:03:50
@Html.ActionLink
生成一个只能用于调用GET的标记。更改为提交按钮,以获得一个帖子。
通常使用一个Edit,您只是在编辑一个sinble模型,而不是在页面上编辑一个集合,但是按照您所拥有的,将cshtml更改为:
@model ICollection<Organization>
<table>
@foreach (var item in Model)
{
using (Html.BeginForm())
{
var test = item.PartyId;
<tr id="@test">
<td class="txt">
<input type="text" name="Caption" class="txt" value="@item.Caption"/>
</td>
<td class="txt">
<input type="text" name="NameInUse" class="txt" value="@item.NameInUse"/>
</td>
<td class="txt">
<input type="text" name="Description" class="txt" value="@item.Description" />
</td>
<td>
<input type="hidden" name="PartyId" value="@item.PartyId"/>
<button type="submit">Edit</button>
</td>
</tr>
}
}
</table>
现在,每个表行都被一个表单包装,这意味着submit按钮将发布该数据。输入上的name
属性将导致MVC模型绑定器正确地将发布的值绑定到模型。
这个隐藏在末尾的输入将确保您的PartyId值被回发。事实上,它是int (而不是可空的),这给您的初始代码带来了异常,我认为。
HTH
编辑
添加控制器代码(注-我仍然认为这有点/很多奇怪,因为您应该只编辑一个Organisation
.
public ActionResult Edit(int id)
{
// get your organisations from your orgRepo... I'm mocking that out.
var orgs = new List<Organization> { new Organization { PartyId = 1, Description = "Org 1", Caption = "Caption 1", NameInUse = "Name 1"},
new Organization { PartyId = 2, Description = "Org 2", Caption = "Caption 2", NameInUse = "Name 2"}};
return View(orgs);
}
发布于 2013-06-12 00:01:44
老天爷,那真是个烂摊子。您的表单中只有一个链接,该链接指向编辑操作,该操作将调用get,表单将永远不会回发。您想在表行中做表单吗?
@foreach (var item in Model) {
var test = item.PartyId;
<tr>
<td colspan ="4>
@using (Html.BeginForm("Edit", "Org", FormMethod.Post))
{
@Html.HiddenFor(modelItem => item.PartyId)
<table>
<tr id="@test">
<td class ="txt">
<input type="text"class="txt" value="@Html.DisplayFor(modelItem => item.Caption)"/>
</td>
<td class ="txt">
<input type="text"class="txt" value="@Html.DisplayFor(modelItem => item.NameInUse)"/>
</td>
<td class ="txt">
<input type="text"class="txt" value="@Html.DisplayFor(modelItem => item.Description )"/>
</td>
<td>
<input type="submit" value="edit" />
</td>
</tr>
</table>
}
</td>
</tr>
}
该代码将在一行中进行编辑,但我只是猜测您发布的代码中的结构。
https://stackoverflow.com/questions/17059552
复制相似问题