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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

15 Comments

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.
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?
Wouldn't that require posting the entire page back each time an attendant was added? I'm a little confused, now.
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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.