I've got problem how to design correctly flow for my controller witch will do some advanced things. I have to have multiple-step adding course in my site. It looks like this:
public class CoursesController : Controller {
[HttpGet]
public ActionResult Create() //1 step - User fill some basic infos and send back forms to Save method
{
return View(model.GetNewInstanceOfCourse());
}
[HttpPost]
public ActionResult Save(NewCourse newCourse) //2 step - Datas are stored in session
{
string Token = Guid.NewGuid().ToString("D");
Session.Add(Token, newCourse);
return RedirectToAction("Subjects", new { Token = Token });
}
[HttpGet]
public ActionResult Subjects(string Token) //2 step - Users fill which Subjects will be on the course, then send forms to Confirm method
{
return View(model.GetAvaliableSubjects(Token/*to place Token in View and let retrieve object from session*/);
}
[HttpPost]
public ActionResult Confirm(Subjects subjects) //3 step - Users filled all required datas and now i want to store complete datas in database
{
//(assume that Session[...] return Dictionaty<string, ... > instead of object
if(!Session["stored-courses-from-first-step"].ContainsKey(subjects.RetrievedFromViewToken)
{
return RedirectToAction("Create");
}
model.AddNewCourse(Session["stored-courses-from-first-step"][subjects.RetrievedFromViewToken], subjects);
return RedirectToAction("Index");
}
}
and it works perfectly... but i have to write adding new subjects for existing course so reuse step 2 in other part of my controller. I have to fill some different datas, then reuse adding subjects for this datas and then reuse function Confirm but instead of inserting some datas i want just update and insert some datas completed from user.
...
public AddNewSubject(int CourseId)
{
...
}
In course preview i have button "Add new subjects", this should bring me to AddNewSubject Method and i don't know what to do next. I can't do in this method something like that:
return RedirectToAction("Save", "Courses", new { newCourse = model.GetExistingCourseAnChangeItToNewCourseInstance(CourseId)})
i don't want to write specialized methods for this due to duplicating most of this code. I think it's possible to reorganize flow in my controller but i doesn't have good idea how to do that. Other problem is that i need to reuse method Confirm, one time it will insert some datas, other time it will update some datas. Maybe you will have some good tips for me.