1

In ASP.NET MVC how can I pass a String variable to the _Layout.cshtml view; ViewBag and ViewData don’t seem to work, is there another/prefered way?

Best Wishes, Yends

2
  • 2
    ViewBag and ViewData should be fine. Can you show how you are going about it? Commented Sep 13, 2011 at 13:38
  • I am trying to pass data from a controller that is not the home controller to the _Layout.cshtml view. Commented Sep 13, 2011 at 13:43

1 Answer 1

4

ViewBag/ViewData work fine. For example:

public ActionResult Index()
{
    ViewBag.Message = "Hello World";
    return View();
}

and inside _Layout.cshtml:

<div>@ViewBag.Message</div>
Sign up to request clarification or add additional context in comments.

4 Comments

I am trying to pass a mock authorisation type from the HTTP-Post Method LogOn inside AccountController to the _Layout.cshtml, this method won't work, do you have any ideas?
@Yends, why it wouldn't work? Suffice to set the ViewBag variable in your POST action to whatever value you want to send. Also in your layout you could test whether the value has been set and only then use it.
My colleague is actually the one working on this and is getting very confused, I will get him to make his own account and continue this thread. Thanks for your input.
continued: stackoverflow.com/questions/7403349/… - sorry for the shoddy use of multiple questions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.