0

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
}
kbaccouche
  • 4,575
  • 11
  • 43
  • 65
  • Create a view that encases your `QuizzModelView` and other members in to one object. Display that object, then submit and accept that view model? (though the way you've described is kind of ambiguous and more detail would be great). – Brad Christie Apr 18 '12 at 13:36
  • Do both views have `QuizzModelView` defined as the model? e.g. `@model QuizzModelView` and passing it off (`View(model: Quizz)`)? – Brad Christie Apr 18 '12 at 13:55
  • Yes, they do. The problem is that when submitting the form in the second view, another instance of QuizzModelView is created so the previous values are lost. – kbaccouche Apr 18 '12 at 14:00
  • So can you post the actions as well? – Brad Christie Apr 18 '12 at 14:00

2 Answers2

0

If you want your TAFPart3PopupEvents action to have access to the data, you need to store it some place. There are many different options (session, querystring, db), but I think a hidden input (in general) is the easiest.

Brian Cauthon
  • 5,524
  • 2
  • 24
  • 26
0

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;

    ....
}
Leniency
  • 4,984
  • 28
  • 36
  • 1
    If you are not already dependant on session, I'd go with a hidden input over session. – Brian Cauthon Apr 18 '12 at 14:55
  • Yes I know hidden input is simpler, but in the question I put a simple version of my code, actually I have more than 30 fields and more than 2 views, so in the final view I will have really a lot of hidden inputs. Won't that be a problem ? – kbaccouche Apr 18 '12 at 16:02
  • With 30+ fields, session sounds easier to me. Or dropping each step in a temp database table (assigned to the logged-in user in case they leave in the middle), but that's a whole different case. If each step is incrementally adding more hidden fields, then things like changing a field name will be a bigger, more error prone pain to update. Typically, if you can avoid storing application state in the view, that's a good thing. – Leniency Apr 18 '12 at 16:32
  • I agree 30+ fields is probably too much to pass around through hidden inputs. However, it also sounds like a lot of input to lose if the session gets invalidated. If angry users (due to lost input from session expiration) are a possiblity, I'd look in to something more permanent than Session, like temporarily saving it to a db. – Brian Cauthon Apr 18 '12 at 18:40