I have two views : the first one has a form which when submitted, it fills in a model view (QuizzModelView).
Now after submitting, I am redirected to another view which also has a form that I want to submit. The problem is that I want to use the same QuizzModelView for the two views. That means, when submitting the second form, I want also to submit the values of the previous form. I can do this by creating hidden inputs which take the values that come from the first view.
Is there a way to do it without hidden inputs.
Thanks
EDIT : To explain more:
My model view contains : QuizzModelView.field1, QuizzModelView,.field2
1st step : View1 will fill in QuizzModelView.field1
2nd step : I am redirected to view2
3rd step : View2 will fill in QuizzModelView.field2
Now I want to be able to get QuizzModelView.field1 and QuizzModelView.field2. But I get Only QuizzModelView.field2 because QuizzModelView.field1 is lost when submitting View2
Here are my actions :
[HttpPost]
public ActionResult TAFPart2PopupEvents(QuizzModelView model)
{
return PartialView("PartialViews/_TAFPart2PopupEvents", model);
}
[HttpPost]
public ActionResult TAFPart3PopupEvents(QuizzModelView model)
{
// here I want to use
// model.field1 and model.field2
}
Technically (pedantically), you won't be able to use the same instance of the model. However, you can put it in the session and pass across the redirects. Session has the advantage of not getting tampered with as easily as hidden fields. Plus you wouldn't have to actually bind the whole model for each step - just the single field from each step:
[HttpPost]
public ActionResult TAFPart2PopupEvents(string field1)
{
QuizzModelView model = new QuizzModelView();
model.Field1 = field1
Session["Quiz"] = model;
return PartialView("PartialViews/_TAFPart2PopupEvents", model);
}
[HttpPost]
public ActionResult TAFPart3PopupEvents(string field2)
{
var model= (QuizzModelView )Session["Quiz"];
// Fill in field2 here
model.Field2 = field2;
}
Edit: To address Brian's comment with some actual detail -
This method with sessions is less susceptible to data tampering than hidden fields, if that's a concern at all. With hidden fields in the view, a malicious user could easily overwrite previous data. Depending on the size of your model, hidden fields could bloat up the view a bit too.
Sessions also have the drawback of expiring. Here's a simple way to handle expirations. If this is called via Ajax, then you'll have to pass an error message back to the client instead to handle there.
[HttpPost]
public ActionResult TAFPart3PopupEvents(string field2)
{
var model= Session["Quiz"] as QuizzModelView;
if (model == null)
{
// Add some kind of message here.
// TempData is like Session, but only persists across one request.
TempData["message"] = "Session Expired!";
return RedirectToAction("Index");
}
// Fill in field2 here
model.Field2 = field2;
....
}