I have an asp.net mvc app with a form.
When you submit the form, it adds records to the sql database with linq-to-sql. After adding the records, the controller displays the form again, and should show those new values on the form. But, when it displays the form, the values are blank, until you refresh the page.
While tracing through the code, I can see the records being added to the database when they are submitted, but the view doesnt display them, unless I refresh. The view is not the problem, it just displays the view model, which is missing the new records immediately after the post.
I know this is kind of vague, but wasnt sure what parts of code to include here.
Could this have something to do with data context life cycle? Basically, there is a data context created when the form is posted, then a different data context is created in the method that displays the form.
Any suggestions on what might be causing this?
Update:
There's a whole lot of code I could post here, but I'll try to give you a simplified version:
This code maintains a schedule of volunteer assignments
The view uses a view model with a list of schedules, and displays a form of schedules and their associated assignments. (child records)
When the form is posted, with a new schedule & assignemnts, a schedule record is created, and the related assignment records are created.
// Controller
public class SchedulerController : Controller
{
ScheduleServices ScheduleSvc = new ScheduleServices(); // creates a new data context
public ActionResult Index()
{
return ShowSchedules();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection form)
{
ScheduleSvc.ProcessRequest(form);
return Index();
}
}
public ActionResult ShowSchedules()
{
SchedulerViewModel sched_vm = new SchedulerViewModel();
sched_vm.EventsAndSchedules = ScheduleSvc.GetEventSchedulesFromDate();
return View(sched_vm);
}
ProceessScheduleRequest(ScheduleRequest req)
{
CreateSchedule(req);
AssignmentServices AssignmentSvc = new AssignmentServices(); // creates it's own data context
AssignmentSvc.Assign(req);
}