How to pass values from One controller to another Controller in ASP.Net MVC3

后端 未结 4 694
青春惊慌失措
青春惊慌失措 2021-01-14 08:19

Hello In my project I have to pass a welcome message with username to the Index Page Its a MVC3 ASP.Net Razor project

There are two controllers are there; O

4条回答
  •  别那么骄傲
    2021-01-14 09:05

    1. Change the Index() method of Home Controller to this:

      [HttpPost]
      
      public ActionResult Index(string username)
      {
           ViewBag.user=username; 
           return View();
      }
      
    2. Modify the Login Controller :

      if (DataAccess.DAL.UserIsValid(model.UserName, model.Password))
      {
          FormsAuthentication.SetAuthCookie(model.UserName, false); 
          return RedirectToAction("Index", "Home",new { username = model.Username } ); 
          //sending the parameter 'username'value to Index of Home Controller
      }
      

    Go to the View Page of the Index method of Home Controller and add the following:

     

    User is: @ViewBag.user

    And you're done. :)

提交回复
热议问题