1

I am unable to access value of Viewbag.IsTeacher=i; in view. What i am doing wrong or there is some thing more to do to access value of view bag ? I want to add an 'if' condition depending on the value of Viewbag.IsTeacher=i;. Spend hours on this but could not get any proper solution.

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{
    if (ModelState.IsValid)
    {
        // Attempt to register the user
        try
        {
            //UserProfile u = new UserProfile();
            WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
            WebSecurity.Login(model.UserName, model.Password);
            //var vRole = Request["selectRole"];
            //u =db.UserProfiles.Find(WebSecurity.CurrentUserId);
            var vRole = Request["selectRole"];
            int i = 0;
            if (vRole.Equals("teacher"))
            {
                i = 1;
                //ViewBag.rolee = "teacher";
                //u.IsTeacher = 1;
            }
            else
            {
                i = 0;
            }
            ViewBag.IsTeacher = i; 
            //ViewBag.rolee = "student";

            return RedirectToAction("Index", "Home");
        }
        catch (MembershipCreateUserException e)
        {
            ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
        }
    }

    // If we got this far, something failed, redisplay form

    return View(model);
}

View:

@if (Request.IsAuthenticated && ViewBag.IsTeacher == 1)
{
    <li>@Html.ActionLink("Quiz Management", "Index", "Quiz")</li>
    <li>@Html.ActionLink("Browse Quizzes", "Index", "TakeQuiz")</li>
    <li>@Html.ActionLink("Assignment Scheduling", "Index", "Assignment</li>
}
Craig W.
  • 17,838
  • 6
  • 49
  • 82
NN796
  • 1,247
  • 11
  • 31

2 Answers2

1

ASP.NET MVC TempData dictionary is used to share data between controller actions. The value of TempData persists until it is read or until the current user’s session times out. Persisting data in TempData is useful in scenarios such as redirection, when values are needed beyond a single request.

In your Register action you would have:

TempData["Role"] = Request["selectRole"];

In your Index action you would have:

ViewBag.IsTeacher = (TempData["Role"] != null && TempData["Role"] == "teacher");

Then in your view you would have:

... ViewBag.IsTeacher == true ...
JBrooks
  • 9,901
  • 2
  • 28
  • 32
  • No, TempData is to pass data between actions, ViewBag is between action and view. So in your second action have ViewBag.IsTeacher = (TempData["IsTeacher"] == "1"); – JBrooks Feb 09 '15 at 16:41
  • I am facing this ERROR now: Object reference not set to an instance of an object. At line: @if (Request.IsAuthenticated && TempData["IsTeacher"].Equals("1")). System.NullReferenceException: Object reference not set to an instance of an object. – NN796 Feb 09 '15 at 16:42
  • please explain your comment more ? what should I do ? – NN796 Feb 09 '15 at 16:46
  • By home action you mean Index action method in my home controller ? or something else ? – NN796 Feb 09 '15 at 17:05
  • You are right, the Index action. I made the change. – JBrooks Feb 09 '15 at 17:21
  • I am currently working in AccountController , i have now index action method in AccountController , but i have Index action method in HomeController ! – NN796 Feb 09 '15 at 17:36
  • still not getting rid of this problem ! :( – NN796 Feb 09 '15 at 18:16
1

When you Redirect, the ViewBag data ceases to exist. When using ViewBag you should return a View only. Use TempData for redirects.

See this similar question Why isn't viewbag value passing back to the view?

Community
  • 1
  • 1
planetregin
  • 111
  • 2
  • 4
  • I am facing this ERROR now: Object reference not set to an instance of an object. At line: @if (Request.IsAuthenticated && TempData["IsTeacher"].Equals("1")). System.NullReferenceException: Object reference not set to an instance of an object. – NN796 Feb 09 '15 at 16:42
  • still not getting rid of this problem ! :( – NN796 Feb 09 '15 at 18:21
  • @JBrooks How can send temp data to controller from view? when I want test to get active code on db ,always it return null. why? this link: ....localhost:44385/account/ActiveEmailAccount/ddd8915deba74659be3ca89fdc118f14 – hasti.au Sep 15 '22 at 07:50