How to check TempData value in my view after a form post?

后端 未结 3 786
闹比i
闹比i 2020-12-31 04:27

I fill my TempData from a FormCollection and then I try to check the value of my TempData in my view with MVC 4 but my if statement is

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-31 05:11

    Try change TempData.Add("var", "abcd");

    to

    TempData['var'] = "abcd";
    

    Update:

    In My controller:

    public ActionResult Index()
        {
            TempData["var"] = "abcd";
            return View();
        }
    

    In my view:

    // I cast to string to make sure it's checking for the correct TempData (string)
    @if ((string)TempData["var"] == "abcd")
    {
       Check
    }
    else
    {
       @TempData["var"].ToString()
    }
    

提交回复
热议问题