1

How do I call the following from within the Login action of the Account controller?

    return View("~/Views/Shared/MyView.cshtml");

When I do I am just returned to "/Account/Login" instead of going to "MyView".

My Login action calls out to an external web service to see if a user is valid (my app doesn't hold the login data. Another app holds it). If the external web service authenticates the user's username and password, it returns a profile object for the user. I want to pass that user profile object to my next app view. I'd use RedirectToAction but I don't see a way to pass the profile data through that. I'm thinking maybe I can just return my next view direclty if I make it shared. I could pass a model to that.

Am I thinking of this the wrong way?

UPDATE:

It's been suggested in comments that what I really want is RedirectToAction(). But what I'm reading suggests this will not work either.

What I'm really after is typed, complex object data accessible from my next app view (after user authentication). "Complex" is the key word here. What I get back after user authentication is a profile object that contains numerous lists (not just simple properties). According to what I'm reading, passing such nested list data from controller to view is just not built into the MVC framework. It's just not an intended use. What views want from controllers is a set of simple properties. Here's a quote from a response to a similar post:

"Internally the RedirectToAction() method builds a RouteValueDictionary by using the .ToString() value of each property in the model. However, binding will only work if all the properties in the model are simple properties and it fails if any properties are complex objects or collections because the method does not use recursion."

I'm thinking I've got to be wrong in my assertion above. It's too sophisticated a technology for this to be the case. I'm just missing something obvious. Can somebody give me some idea of how to accomplish what I'm after here?

Robert
  • 828
  • 2
  • 13
  • 28
  • Authenticate the user, store the object in Session and redirect.. – ChrisBint Dec 19 '16 at 21:22
  • 1
    `return View()` does not change the url. For that you need `return RedirectToAction()` –  Dec 19 '16 at 21:25
  • Thanks for responding. As I indicated, RedirectToAction will not take a model. This will not work for me. – Robert Dec 19 '16 at 22:49
  • I missed the part about storing object in session first. Will that allow passing of complex objects, or just name value pairs. Need complex object. Examples anywhere? Isn't there any way to just pass a model? – Robert Dec 19 '16 at 22:56
  • Of course `RedirectToAction()` can take a model –  Dec 19 '16 at 23:53
  • If you want that object in Action method then you can also use TempData – Khalid Dec 20 '16 at 10:21
  • See update above... – Robert Dec 21 '16 at 01:30
  • Then persist your object somewhere (database, `Session` etc) and just pass its `id` to the other method so that you can get it again. –  Dec 21 '16 at 07:34

1 Answers1

0

If its possible, use an overload that takes your model on the same controller.

But if it is completely different controller, you could try the following suggestions in this post, such as using TempData:

How do I include a model with a RedirectToAction?

Community
  • 1
  • 1
MikeDub
  • 5,143
  • 3
  • 27
  • 44