views:

48

answers:

3

Hi everyone.

I'm reading Steven Sanderson's book Pro ASP.NET MVC 2 Framework. In his book, he use multiple web forms on a single web page to list products from database and provide Add to cart functionality. When should I take this approach vs having single web form and using jquery selectors to add certain products to shopping cart?

Thank you for your help!

A: 

This allows you to limit the data you post back so that it's relevant only to the Model you're updating. In the case of that book, each item in the list corresponds to a list item in the Model. So to answer your question, any time you have an item on your page that represents something in your Model, if you need it to have form behaviour (e.g. you need to submit its data), wrap it in form tags.

If you were using jQuery selectors to post the selected items contained within a single form, it would effectively be the same thing with just a bit more work to inspect each element and post the selected items.

DaveDev
Does this solution have any drawbacks such as performance, incompatibility with some jquery plugins, etc?
šljaker
posting the form has nothing to do with jQuery, so in that respect, no. However, if you're posting selected items in the form, you need jQuery (or some other javascript) to do that.
DaveDev
A: 

I think that it would help if JavaScript is disabled. I'm not familiar with that example, but if you've got multiple forms, a submit would post only the relevant data whether you've got scripting on or off.

Andorbal
+1  A: 

In ASP.NET MVC you no longer limited to one unique HTML form, so you can have several forms on your page that call different controllers, this allows you to have a more interactive and dynamic UI.

You should take the multiple form approach when your UI design benefits from having several forms, try to get out of the single form mindset and check if your UI would benefit from it, if you see some of the web 2.0 sites you´ll notice that they don´t restraint themselves to the one form per page restriction that exists in traditional ASP.NET

Now if your UI is simple and it does only one task you don´t need several forms, one just would do.

Manuel Castro
+1 for great answer
šljaker