2

Please feel free to edit the title if it is misleading, since I am not sure if that's the correct way to ask.

I am new to ASP.NET MVC I am running into a seemly easy problem and having a hard time doing it.

What I am trying to do: I have two sets of scaffolds: View A, Controller A, Model B. and View B, Controller B, Model B.

Controller A looks like this:

public ActionResult LogIn(FormCollection formValues) { ModelA Model = new ModelA(); Model.EmailAddress = formValues["EmailAddress"]; } 

I want to pass this formValues["EmailAddress"] from controller A to controller B.

In controller B, I have:

public ActionResult Initiate(FormCollection formValues, string phone, string method) { var ModelB = new ModelB(); var ModelA = new ModelA(); ---> This is null. ModelB.Email = ModelA.EmailAddress --> This is null. var userId = ModelB.dosomething(ModelB.Email, phone, method); } 

Is there away of doing that?

7
  • You can use Session["EmailAddress"] = ModelA.EmailAddress;CommentedMar 1, 2019 at 19:47
  • @chakeda I am assuming that the session is similar to TempData. It seems like it is a one-off thing. Is there a natural way that fits the work flor or MVC?
    – ZpfSysn
    CommentedMar 1, 2019 at 19:48
  • 1
    How is one controller calling the other? By using Redirect? Action?
    – D.R.
    CommentedMar 1, 2019 at 19:56
  • @D.R. Currently, they are completely seperated. However, in the future, View B is right after View A(navigated by buttons). I havn't figure out a way to do that either sadly, was going to to that after I finished this. Now you mentioned it, it sounds like it may be a better idea to connect two views first to creat a work flow.
    – ZpfSysn
    CommentedMar 1, 2019 at 20:03
  • @aDev Having data persist between requests from A to B is what Sessions are for - I would argue that posting data between different controllers is not the intended flow of MVC. You could have A and B share functions (dosomething()), or call dosomething() in Login().CommentedMar 1, 2019 at 20:11

3 Answers 3

1

If the logic of ModelA and ModelB are going to be intertwined, I would recommend creating a new ViewModel, View, and Controller:

ModelAB

public class AB { public ModelA ModelA { get; set; } public ModelB ModelB { get; set; } } 

ModelABController

public ActionResult LogInAndInitiate(FormCollection formValues, string phone, string method) { var ModelB = new ModelB(); var ModelA = new ModelA(); // passed in, so its not null! ModelA.EmailAddress = formValues["EmailAddress"]; ModelB.Email = ModelA.EmailAddress // passed in too! var userId = ModelB.dosomething(ModelB.Email, phone, method); } 
    1

    Save the Model in TempData in Controller A like this

    ModelA Model = new ModelA(); Model.EmailAddress = formValues["EmailAddress"]; TempData["ModelA"]=Model; 

    and Then You can Acces it in Controller B like this

    ModelA modelA= TempData["ModelA"] as ModelA; 

    However TempData has very short life and can not be used in subsequent requests to keep the TempData persistant for subsequent request you can use

    TempData.Keep(); 
    2
    • You should explain that TempData values have a very short lifetime
      – Steve
      CommentedMar 1, 2019 at 19:37
    • is this the only way in ASP.MVC? seems kind of hacky. Is there an approach that betters fits the MVC flow?
      – ZpfSysn
      CommentedMar 1, 2019 at 19:46
    1

    If the controller actions are completely unrelated (i.e., they do not call each other) you have two options:

    1) Render the data to the client into a form and let the data be re-posted on the next request. This is of course only suitable for small amounts of data AND if you are sure it is neither sensitive nor a problem if the user tampers with the data.

    2) Use sessions. This is what sessions are for, i.e., shopping cart contents, etc. You can opt to save this data in a database or have it in-memory in a session object only.

    I wouldn't recommend using TempData for this, the idea behind TempData is to contain data which is useful for the very next request only. Think validation messages and that kind of data.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.