ASP.NET MVC Beta Ajax Update Issue

I have ever waited to bring the Asp.net Preview 4 project to sniff, completely skipping Preview 5 just because I knew that I was having problems.

In any case, here is the question and the dilemma.

I have several areas on the site that have an ajax update type panel that displays content from a view using this technique found here. AJAX panels with ASP.NET MVC

This worked fine in preview 4, but now in beta I get this all the time.

Sys.ArgumentNullException: Value cannot be null Parameter name eventObject 

It made me nuts ...

My code is as follows

 <% using (this.Ajax.BeginForm("ReportOne", "Reports", null, new AjaxOptions { UpdateTargetId = "panel1" }, new { id = "panelOneForm" })) { } %> <div class="panel" id="panel1"><img src="/Content/ajax-loader.gif" /></div> <script type="text/javascript"> $get("panelOneForm").onsubmit(); </script> 

so basically what it does is force feed in a form that updates panel1 with the content from the ReportOne view.

What am I missing? Why am I getting this error? Why did they go and change something? I love MVC, but it makes me crazy.

+4
source share
3 answers

Some annoying problems associated with this problem. Hope someone here can help me.

 var event = new Object(); function refreshInformation(){ document.forms['MyForm'].onsubmit({preventDefault: function(){} }); } 

This is my current code, it is great for updating the form. The problem is that the "var event" violates all other javascript events, if I have, for example, the following:

 <img src="myimg.gif" onmouseover="showmousepos(event)" /> 

this is not a mouse event that is dispatched to a function, instead it is my "var event" that I must declare in order to make onsubmit function properly.

When using only

 onsubmit({preventDefault: function(){} } 
without "var event" i get
 Sys.ArgumentNullException: Value cannot be null Parameter name eventObject 

I also tried using

 submit() 
, it does a full postback and completely ignores the ajaxform stuff ... at least in my solution.

Hmm ... I understand this can be a little confusing, but if someone understands the problem, it would be great if you had a solution. equals sign If you need more information about a problem, just ask and I will try to clarify which is more.

0
source

Unfortunately, just calling submit () will not fire the onsubmit event, so the MVC Ajax script will not fire. When the browser calls onsubmit () for you (because the user clicked the submit button), it actually provides a parameter called event (which you can see if you look at the Html provided by the Ajax helper),

So, when you call onsubmit () manually, you need to provide this parameter (because it requires MVC Ajax code). So what you can do is create a "fake" event parameter and pass it to onsubmit:

 <% using (this.Ajax.BeginForm("ReportOne", "Reports", null, new AjaxOptions { UpdateTargetId = "panel1" }, new { id = "panelOneForm" })) { } %> <div class="panel" id="panel1"><img src="/Content/ajax-loader.gif" /></div> <script type="text/javascript"> $get("panelOneForm").onsubmit({ preventDefault: function() {} }); </script> 

The important part is the {preventDefault: function () {}} section, which creates a JSON object that has a "preventDefault" method that does nothing. This is the only thing the MVC Ajax script does with the event object, so this should work fine.

Perhaps a longer fix would be if the MVC Ajax code had a check that simply ignored the null event parameter (wink @Eilon: P)

+1
source

I believe that calling someFormElement.onsubmit () just calls the event handlers registered for this event. To submit the form correctly, you must call someFormElement.submit () (without the "on" prefix).

I don’t think we have changed something in the behavior of AJAX helpers between ASP.NET MVC Preview 4 and ASP.NET MVC Beta.

Thanks Eilon

0
source

All Articles