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?
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>