Search code examples
ajaxasp.net-mvcasp.net-mvc-3asp.net-mvc-ajax

The error of can not find View in Ajax form


I ask a similar question here

So I add Some OnComplete Functions and Id to Ajax Forms, And there is:

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,
OnComplete = "OnCompleteEditPhone"
}, new { id = "EditAjaxForm" + Model.Id})) {
<td>@Html.DisplayFor(modelItem => Model.PhoneNumber)</td>
<td>@Html.DisplayFor(modelItem => Model.PhoneKind)</td>
<td><input type="submit" value="Edit" class="CallEditPhone" id="edit@(Model.Id)" /></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,
OnComplete = "OnCompleteSavePhone"
})) {
<td>@Html.EditorFor(modelItem => Model.PhoneNumber)</td>
<td>@Html.EditorFor(modelItem => Model.PhoneKind)</td>
<td><input type="submit" value="Save" class="SaveEditPhone" id="save@(Model.Id)" /></td>
}

And Oncomplete Scripts:

function OnCompleteEditPhone() {

$('input.SaveEditPhone').click(function () {
    var id = $(this).attr("id").substring(4);
    $('form#SaveAjaxForm' + id).trigger('submit');
});
}

function OnCompleteSavePhone() {
$('input.CallEditPhone').click(function () {
    var id = $(this).attr("id").substring(4);
    $('form#EditAjaxForm' + id).trigger('submit');
});
}

So Click Edit Worked perfect, Then Click Save Worked good also, But in second time when i click the Edit Button I have an Error in Post Action I copy the Firebug console here:

http://Mysite/members/editphone/7652 200 OK 582ms
http://Mysite/members/savephone/7652 200 OK 73ms
http://Mysite/members/editphone/7652 500 internal server error 136ms
<title>The view 'EditPhone' or its master was not found or no view engine supports the searched locations. The following locations were searched: ...

So where is the problem? If I remove OnCompleteSavePhone The Edit button for second time not worked, and with this function I have an error that not make any sense, How Can I fix it? I actually load partial views by Ajax, And need the buttons of this views worked correctly, at first every thing is fine but after Ajax result They don't, I think to add some Oncomplete functions, but there is an error also.


Solution

  • Your previous question is answered now. You had broken markup. As a consequence of this you no longer need to care about any OnComplete events and doing some auto triggers, form submissions and stuff. This will be handled by the Ajax.BeginForm infrastructure automatically for you.