从Razor视图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 '的参数'id'的空条目。为方法'System.Web.Mvc.ActionResult Edit(Int32)'在'Dwiza.Controllers.OrgController'。可选参数必须是引用类型、可空类型,或者声明为可选参数。参数名称:parameters

我的问题:

  1. 我该如何解决这个问题?
  2. 为什么它得到编辑动作被调用,而不是后编辑?
  3. 通过jQuery或ajax或任何其他调用调用编辑动作的更好方法是什么,如果你能提出更好的方法?

@Html.ActionLink产生一个只能用于调用GET的a标记。修改为提交按钮来获取POST。

通常使用编辑,您只编辑单个模型,而不是页面上的集合,但根据您所拥有的,将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);
}

天哪,这真是一团糟。您的表单中只有一个链接,该链接指向编辑操作,该操作将调用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>   
}

该代码将在一行内进行编辑,但我只是猜测您发布的代码结构。

最新更新