views:

169

answers:

1

In an ASP.NET MVC application where I have the following model..

public class EventViewModel
{
 public Guid Id { get; set; }
 public List<Guid> Attendants { get; set; }
}

passed to a view...

public ActionResult Create(EventViewModel model)
{ 
 // ...
}

I want to be able to add to the "Attendants" Collection from the View. The code to add it works just fine - but how can I ensure that the View model retains the list? Is there any way to do this without saving/opening/saving etc? I am calling my addition method via jQuery $.load.

public void Insert(Guid attendant)
{
 // add the attendant to the attendees list of the model - called via $.load
}

I would also like to add there is no database. There is no storage persistence other than plain files.

+1  A: 

If you are posting your data and want to re-display it, you have to round-trip your data back to the view.

To maintain multi-user data integrity, I typically save the data to the database, and then retrieve the data from the database again when passing it back to the view for redisplay.

Potentially, you can do something AJAX-y in the view to add records, so that you don't have to continually round-trip the entire dataset each time a record is added.

EDIT: Just noticed that you don't have a database. If your application is architected properly (i.e. you are using repositories), the method of backend storage shouldn't matter.

For more info on general practices for adding records, see the NerdDinner tutorial.

Robert Harvey
I have repositories. But the event id doesn't exist until the event is created - therefore the file for the event doesn't exist until then - yet the attendance list needs the event id. I could psudo-generate it, but then there are persistence and postback issues.
Stacey
Your ActionResult is accepting the entire ViewData object. Can't you just create and persist the Event object as needed, prior to adding the attendants?
Robert Harvey
Wouldn't that require posting the entire page back each time an attendant was added? I'm a little confused, now.
Stacey
Once you're in your `Create` controller method, you can determine whether or not the Event already exists, create and persist the event if it doesn't exist yet, retrieve the event if it already exists, add attendants as necessary, persist those, and return your view.
Robert Harvey
And yes, if you only have room on your page for adding a single attendant, you will have to do the round trip each time.
Robert Harvey
The problem is that, if you want to add multiple attendants without POSTing, you need a way to dynamically add controls to your form to accomodate the additional attendant records. You can do that with jQuery if you wish.
Robert Harvey
Wait, I'm lost. I can add an attendant without posting?
Stacey
I'm using jQuery UI Sortable Lists - dragging from a roster box into the attendants. You can drag as many as you want, but it posts each 'drop' back to the server to update the list. Is there a better way to do this?
Stacey
oh! Idea!! How could I send the UL that attendees are dragged into into the postback? The attendance list can just be made in the HTML DOM, and sent over. How would I go about injecting that into my postback model?
Stacey
It would take me awhile to sift through the jQuery UI documentation, but I would imagine there is a way you can tell jQuery UI to not do the postback on the drop, and then you can provide your own Submit button for the postback.
Robert Harvey
Yes, you've got the idea. There is a post that shows how to bind these sort of things...Hold on while I look it up.
Robert Harvey
So then I need to figure out how to send a ul collection on pressing a submit button. I should open up another question for this, then? Thanks again for all of your help - this is going somewhere.
Stacey
Alright, I'll wait to see what post you are referring to.
Stacey
Here it is: http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx
Robert Harvey
...Although it might not help you much if your UI elements are UL elements and not INPUT elements.
Robert Harvey
Yeah, I was just reading that. Still, this is a foundation. Thanks again, I'll be opening another more specific question next with this as a good reference.
Stacey