Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I'm returning a json data, and I can confirm that it is bringing data back to client. but instead of updating my jqueryaccordion, it asks me to save or open the file. Below is my script and controller. I have used jquery modal dialog to edit the employee details through a partial view, and clicking on update button should update the respective employee in the accordion list.Any help would be greatly appreciated - thanks

When debugging through IE tools, I noticed that when the 'update' button is clicked, the 'initiator' in the 'request' tab shows 'click'. I am guessing this should be 'XMLHttpRequest' Instead. Hope this information helps. Thanks


**Main View**

HTML
@Html.ActionLink("Edit Employee", "EditEmployee", "Home",
                        new { id = item.Id }
                        , new { @class = "editLink" })


**Partial View with Edit Employee Form - EditEmployee.cshtml**


HTML
@using (Ajax.BeginForm("EditEmployees", "Home", new AjaxOptions
         {
             InsertionMode = InsertionMode.Replace,
             HttpMethod = "POST",
             OnSuccess = "updateSuccess"
         }, new { @id = "updateEmployeeForm" }))
     {
         <div class="editor-label">
             @Html.LabelFor(model => model.Name)
         </div>
         <div class="editor-field">
             @Html.TextAreaFor(model => model.Name)
             @Html.ValidationMessageFor(model => model.Name)
         </div>

 }


**Action Result that returns partial view containing the editemployee form**


C#
Public ActionResult EditEmployee(int id)
   {
   //DataAccess part
   return PartialView("EditEmployee",employeedata);
   }


**Controller that returns Json Result after updating the employee details**

C#
[HttpPost]
Public JsonResult EditEmployee(Models.Employee employee)
{
       //Data access part

     JsonResult result = new JsonResult();
                            result.Data = employeeData;
                            return result;
}


**Script on Main View**

JavaScript
  <script type="text/javascript">
           var linkObj;
           $(function () {
               $(".editLink").button();

           $('#updateDialog').dialog({
               autoOpen: false,
               width: 400,
               resizable: false,
               modal: true,
               buttons: {
                   "Update": function () {
                       $("#update-message").html('');
                       $("#updateEmployeeForm").submit();
                   },
                   "Cancel": function () {
                       $(this).dialog("close");
                   }
               }
           });

           $(".editLink").click(function () {
               //change the title of the dialog
               linkObj = $(this);
               var dialogDiv = $('#updateDialog');
               var viewUrl = linkObj.attr('href');
               $.get(viewUrl, function (data) {
                   dialogDiv.html(data);

                   var $form = $("#updateEmployeeForm");
                   // Unbind existing validation
                   $form.unbind();
                              dialogDiv.dialog('open');
               });
               return false;
           });

       });

function updateSuccess(data) {
// I want to make sure that this function gets executed on Success instead of a file being sent back to me from the server
 $('#updateDialog').dialog('close');
 $('#commonMessage').html("Update Complete");
 $('#commonMessage').delay(400).slideDown(400).delay(3000).slideUp(400);
 alert("hello");

       }
Posted
Updated 12-Apr-13 0:38am
v4
Comments
Moykn 11-Apr-13 13:59pm    
is this right? @using (Ajax.BeginForm("EditEmployees", "Home", new AjaxOptions shouldn't be @using (Ajax.BeginForm("EditEmployee", "Home", new AjaxOptions ?
devdev13 12-Apr-13 6:37am    
Yeah. That's right. It is spelt correctly in the code. Thanks. Any other suggestions ?

Hi,

This normally occures if you try to post a form in an IFrame and the result is JSON (mostly used in Ajax-fileuploads.

The problem is (in the above mentioned scenario) that IE does not detect the JSON result as a text to display, so it assumes that you want to download it.

To circumvent this behaviour, you can simply change this:

C#
JsonResult result = new JsonResult();
result.Data = employeeData;
return result;


into this:

C#
JsonResult result = new JsonResult();
result.Data = employeeData;
result.ContentType = "text/plain";
return result;


After this change, IE will assume that he will get Text, so it displays it correctly.

Give it a try, perhaps this also solves your problem :)
Hope this helps.

Best regards and happy coding,
Chris
 
Share this answer
 
Probably you have forgotten to add jquery.unobtrusive-ajax.min.js to your page, check it please.

HTML
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900