Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Ajax.BeginForm work in Chrome?

I'm working with c#.NET MVC2 and I'm trying to create an ajax form that calls a method that deletes a database record (RemoveRelation). The process of deleting the record is working as intended. After the record is deleted the form should call a javascript function that removes the record from the visuals (RemoveRelation(10)). This is done through an AJAX call that on Internet Explorer 9 and Firefox 4 are all working as intended however on Chrome for some reason the update is not happening throuhg an AJAX call and the whole page is refreshing when the form to delete the record is being submitted (this is incorrect as the form is supposedly being generated with AJAX functionality). This is the code with which I am generating the form:

 <% using (Ajax.BeginForm("RemoveRelation", "Relations",
       new AjaxOptions { OnSuccess = "function() { RemoveRelation(10); } ", InsertionMode = InsertionMode.Replace, UpdateTargetId = "Relation10" },
       new { id = "DeleteForm10" }))
   { %>

Additionally on Chrome I have another issue with a separate Ajax.BeginForm.

<% using (Ajax.BeginForm("AddRelation", "Relations", new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "AddRelation" }, new { id = "AddRelationForm" }))
  { %>

The above Begin Form code is used to add Relations to the list instead of the removing them. Once again I stress, on IE9 and FF4 the above is working as intended, on chrome instead of adding one and updating through ajax, it's instead adding the record twice and once again refreshing the whole page rather than doing the ajax update.

Why is this breaking down in chrome?

like image 980
William Calleja Avatar asked Feb 16 '12 10:02

William Calleja


People also ask

When to use Ajax BeginForm?

Ajax. BeginForm is the extension method of the ASP.NET MVC Ajax helper class, which is used to submit form data to the server without whole page postback.

What is the difference between Ajax BeginForm and HTML BeginForm?

BeginForm() will create a form on the page that submits its values to the server as a synchronous HTTP request, refreshing the entire page in the process. Ajax. BeginForm() creates a form that submits its values using an asynchronous ajax request.

What is the purpose of UpdatetargetId when specifying an Ajax form?

UpdatetargetId is the id of the DOM element which you want to update depending upon repsonse from the server. If you are returning any View or PartialView from controller using Ajax. BeginForm . This DOM element will be updated and will have view content you have returned.


2 Answers

There's an issue with Microsoft AJAX and certain webkit browsers. I ran into this issue myself a while ago, and the fix is pretty simple. Create a webkit.js file and put this in the contents:

Sys.Browser.WebKit = {}; //Safari 3 is considered WebKit
if( navigator.userAgent.indexOf( 'WebKit/' ) > -1 )
{
    Sys.Browser.agent = Sys.Browser.WebKit;
    Sys.Browser.version = parseFloat( navigator.userAgent.match(/WebKit\/(\d+(\.\d+)?)/)[1]);
    Sys.Browser.name = 'WebKit';
}

Then either within your ScriptManager or somewhere in your page (preferably in a master page, I added it near the end), add the script reference:

<Scripts>
    <asp:ScriptReference Path="webkit.js" />
</Scripts>
//OR
<script src="webkit.js">

Can't remember the original site that I found the info, but this I was able to get from this page. Good luck!

like image 159
SPFiredrake Avatar answered Sep 21 '22 07:09

SPFiredrake


you may update your project to use asp.net MVC 3 , within mvc 3 , jquery ajax is the default

like image 24
dfang Avatar answered Sep 20 '22 07:09

dfang