AjaxForm in result of AjaxForm

僤鯓⒐⒋嵵緔 提交于 2019-12-01 10:59:42

问题


This is My View:

@foreach(var item in Model) {
 <tr id="TR@(item.Id)">
    @{Html.RenderPartial("_PhoneRow", item);}
 </tr>
}

_PhoneRow:

@model PhoneModel
@using(Ajax.BeginForm("EditPhone", new { id = Model.Id }, new AjaxOptions {
UpdateTargetId = "TR" + Model.Id
})) {
<td>@Html.DisplayFor(modelItem => Model.PhoneNumber)</td>
<td>@Html.DisplayFor(modelItem => Model.PhoneKind)</td>
<td><input type="submit" value="Edit" /></td>
}

Controller:

public ActionResult EditPhone(long Id) {
  //Get model by id
  return PartialView("_EditPhoneRow", model);
}

public ActionResult SavePhone(PhoneModel model) {
  //Save Phone, and Get Updatet model
  return PartialView("_PhoneRow", model);
}

_EditPhoneRow

    @model PhoneModel
@using(Ajax.BeginForm("SavePhone", new { id = Model.Id }, new AjaxOptions {
UpdateTargetId = "TR" + Model.Id
})) {
<td>@Html.EditorFor(modelItem => Model.PhoneNumber)</td>
<td>@Html.EditorFor(modelItem => Model.PhoneKind)</td>
<td><input type="submit" value="Save" /></td>
}

So When I click Edit Button the _EditPhoneRow Replaced Perfectly, but then when I click on Save button there is not any get, where is the problem?, why when updated the row with new partial view the new Ajax form not working? I think this issue is very popular, I just need Edit-Save With Ajax in any row, what is your suggestion? or any source or good sample for it?


回答1:


You have broken markup. It is forbidden to nest a <form> element directly beneath a <tr>. And when you have broken markup, you might get undefined result. In your case this undefined result translates by the fact that when you click on the submit button of the second form the submit event is not raised and nothing happens because the unobtrusive-ajax library lived/delegated for this event. The workaround consists into using another table.

So:

_PhoneRo.cshtml:

@model PhoneModel
<td>
    @using (Ajax.BeginForm("EditPhone", new { id = Model.Id }, new AjaxOptions { UpdateTargetId = "TR" + Model.Id }))
    {
        <table>
            <tr>
                <td>@Html.DisplayFor(modelItem => modelItem.PhoneNumber)</td>
                <td>@Html.DisplayFor(modelItem => modelItem.PhoneKind)</td>
                <td><input type="submit" value="Edit" /></td>
            </tr>
        </table>
    }
</td>

_EditPhoneRow.cshtml:

@model PhoneModel
<td>
    @using (Ajax.BeginForm("SavePhone", new { id = Model.Id }, new AjaxOptions { UpdateTargetId = "TR" + Model.Id }))
    {
        <table>
            <tr>
                <td>@Html.EditorFor(modelItem => modelItem.PhoneNumber)</td>
                <td>@Html.EditorFor(modelItem => modelItem.PhoneKind)</td>
                <td><input type="submit" value="Save" /></td>
            </tr>
        </table>
    }
</td>


来源:https://stackoverflow.com/questions/11372421/ajaxform-in-result-of-ajaxform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!