2013-06-12 51 views
0

这个问题可能有几次被问过,但它不适用于我的情况,所以请耐心等待。来自剃须刀视图的通话后编辑动作mvc4

我在我的控制器以下操作:

[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(); 
    } 

我试图通过调用后编辑操作来更新数据到数据库中。 为此,我呼吁编辑操作如下:

@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.Web.Mvc.ActionResult Edit(Int32)'中的'System.Int32'在'Dwiza.Controllers.OrgController'中。可选参数必须是引用类型,可为空类型,或者声明为可选参数。 参数名:参数

我的问题:

  1. 能有人帮助如何解决这一问题?
  2. 为什么它的编辑动作被调用而不是编辑?
  3. 什么是更好的方式来调用通过jquery调用编辑操作,或ajax 或任何其他,如果你可以建议更好的方法来做到这一点?

在此先感谢//

+0

看起来你失踪设置“方法”参数“后”您

maxs87

回答

0

乖乖,这是一个混乱的家伙。你的表单只有一个链接,并且该链接指向编辑动作,它将调用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> 
} 

该代码会做一个编辑的行里面,但我只是从您发布的代码结构猜测。

+0

这直接击中我的模型类,它不是通过我的控制器编辑行动去..请帮助 – mmssaann

2

@Html.ActionLink产生一个标签,它只能用于调用GET。更改为提交按钮以获取POST。

与编辑

通常情况下,你只能编辑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> 

现在每个表行由包裹这意味着提交按钮将发布该数据。输入上的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); 
} 
+0

即时得到服务器错误,当我尝试上述观点时,你可以建议吗?传入字典的模型项目类型为'System.Data.Entity.DbSet'1 [PartyBiz.Models.Objects.Organization]',但是此字典需要类型为“System.Collections.Generic.ICollection'1的模型项[PartyBiz.Models.Objects.Organization]”。 – mmssaann

+0

您的代码正在遍历模型中的剃刀代码,意思是模型需要是一个组织对象的集合。再次,有点奇怪,因为您通常会编辑单个实例 - 更新我的答案以包含示例控制器代码。 –