Paul Ulvinius Blog

From one developer to another

Archive for March, 2009

How you dynamically submit a ASP.NET MVC AJAX Form from JavaScript and JQuery

Posted by paululvinius on March 3, 2009

I ran into a problem the other day when trying to submit a form generated with Ajax.BeginForm(…). I have an <a> -tag with a click-event, so when you click the <a> -tag the form is submitted. First of all, if you use a submit button or image submit button there is no issue, this only applies if you want to submit the form programmatically from JavaScript. The BeginForm method generates a form with inline JavaScript in the forms onsubmit attribute. The problem you run into is that this code never gets executed when you call the forms submit method directly from JavaScript.

To get around this issue, you can bind a function to the submit event that contains the same code as the code in the onsubmit attribute. You can do this as soon as the DOM is ready to be manipulated:

image

Feel free to copy/paste from here:
$("form").submit(function(event) { eval($(this).attr("onsubmit")); return false; });

The key here is to pass the event parameter (otherwise it will work only in IE but not in FF for some reason), use eval to run the onsubmit code and finally return false to prevent the form from being submitted in a traditional non-Ajax manner.

Now you should be able to submit the form programmatically like below:
<a onclick="$(this).parents(‘form’).submit();return false;">Send</a>
(this code only works if the <a> –tag is located within the form)

If anyone finds another solution to this problem please let me know, but in the mean time this works fine.

Posted in Ajax, ASP.NET MVC, JavaScript, JQuery | 8 Comments »

First thoughts – CRUD scaffolding in the ASP.NET MVC 1.0 Release Candidate (RC)

Posted by paululvinius on March 1, 2009

For quiet a while a was trying to come up with something really good to write about on this first “dramatic” blog post. Finally I came to my senses, that’s not how you blog in the first place! Instead, here is just a random topic from my mind right now, to break the ice if nothing else :)

Credit to the CRUD scaffolding in the ASP.NET MVC 1.0 Release Candidate (RC)! The support for T4 templating is a cool feature and is especially useful in projects where you have lots of pages that shares the same design and layout, or if you’re in a large company and you want everyone to get the same default look and feel when new views are created across projects.

And the but…

Don’t know if I’m the only one, but it seems like my projects never end up being repetitive enough for me to actually use these features. At the end of the day, every new page (or view) I create turn out to share almost nothing with the previous one. Of course this totally depends on the project anyways, they are really awesome features, and maybe I just need to learn how to make better use of them.

Posted in ASP.NET MVC, Visual Studio | Tagged: , , | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.