my current problem includes three classes let's say A, B and C. Class A includes one property of an object of class B and one of class C, a string in this case. the user should create a new object of type A in the first step without these two properties (only the other properties are asked). An incomplete object of type A (let's call it a) should be built and in the next step the user shall decide whether he wants to match this object to an existing B or a new one. After this decision, the needed property is added to a and in the third step, the second property will be added in the same way and to finish it, the object a will be saved in the database.
My current attempt looks like this:
public ActionResult Create()
{
return View();
}
//
// POST: /A/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(A a)
{
if (ModelState.IsValid)
{
return RedirectToAction("ViewForB", new { a = a});
}
return View(a);
}
public ActionResult ViewForB(A a)
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ViewForB(B b, A a)
{
if (ModelState.IsValid)
{
bDb.Bs.Add(b);
bDb.SaveChanges();
a.propertyForB = b.title;
return RedirectToAction("ViewForC", new {a = a});
}
return View(b);
}
public ActionResult ViewForC(A a)
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ViewForC(C c, A a)
{
if (ModelState.IsValid)
{
cDb.Cs.Add(c);
cDb.SaveChanges();
a.propertyForC = c.title;
aDb.As.Add(a);
aDb.SaveChanges();
return RedirectToAction("Index/");
}
return View();
}
As you can see I tried to pass an A object to every next view but when submitting in ViewForB this won't work, because I don't know how to pass it in this case. But in ViewForB objects of type B will also be built, therefore I decided to make it in this way to verify if the inputs for B are correct or not like in the create method.
Do you know how to pass an object of type A to the last view, to complete and safe it there?
Thank you very much, any answer would be helpful.