tags:

views:

37

answers:

2

Hello, using asp.net mvc, I'm initializing a list in the server code, and allowing the end-user to add to the list on the form (using JQuery to add entries). I know how to obtain a list's selected items on a post back, but I don't need to do that here. I want the complete contents of the list accessible in the server code after a post back.

Is a posted list just not going to give me the full content? Should I use Ajax to send each item to the server as each items gets added to the list?

thanks

+1  A: 

There are a couple of ways that I can think of doing this.

  • make an ajax call each time an item is added - as you suggested
  • when an item is added to the list, you also add a hidden field to the form that will be submitted. Then on post back (although that terminology is very Web Forms-y), in your action method, you'll have access to the contents of the list. If you name things correctly you should be able to model bind to a List.

The latter would be my preference, it depends on your particular situation though.

thatismatt
Thanks, I like the idea of a hidden field. Web Forms-y.. that's funny... you're right, that is my web background.
A: 

Some things to keep in mind:

  • Bandwidth from the user to the web server is very small compared to bandwidth from a database to the web server
  • The database most likely cached whatever query you just ran to populate that list
  • It sounds like you're asking for the entire list to be included with the postback data, and that means having the browser upload that data for the user.

With all that in mind, it should be obvious that you're better off rebuilding your base list from the database.

Joel Coehoorn
Thanks for the response. In my case, the end-user is adding content to the list via screen input - the server will not have populated the list. I think I'll go with a hidden field.