Adding items to Model Collection in ASP.NET MVC
In an ASP.NET MVC application w开发者_如何学运维here 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.
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.
精彩评论