RedirectToAction usage in asp.net mvc

后端 未结 4 426
甜味超标
甜味超标 2020-12-28 13:34

I want to post some questions about ASP.Net MVC. I am not familiar with web developing, But I was assigned to the web part of a project. We are doing the follow

4条回答
  •  旧时难觅i
    2020-12-28 13:51

    this may be solution of problem when TempData gone after refresh the page :-

    when first time you get TempData in action method set it in a ViewData & write check as below:

    public ActionResult Index()
    {
        TempData["nickname"] = Person.nickname;
        return RedirectToAction("profile", "person");
    }
    

    now on the Profile action :

    public ActionResult Profile()
    {
        var nickname = TempData["nickname"] as string;
    
        if(nickname !=null)
            ViewData["nickname"]=nickname;
    
        if (nickname == null && ViewData["nickname"]==null)
        {
            throw new HttpException(404);
        }
        else
        {
            if(nickname == null)
                nickname=ViewData["nickname"];
        }
    
        return View();
    }
    

提交回复
热议问题