限制全局变量不应该在 class 初始化之外进行赋值

Restrict the global variable should not take assign value which is done outside the class initialization

我的控制器中有一个全局变量 isSave。

private bool isSave;

同一控制器中有Get方法。我希望下面给出的 viewbag 应该是 true/false 基于条件

ViewBag.EmailConfirmationSent = false;

[HttpGet]
public ActionResult Index()
{
    if (!isSave)
        ViewBag.EmailConfirmationSent = false;
}

同一个控制器中有post方法

[HttpPost]
public ActionResult Index(ProfileViewModel profileViewModel)
{
    bool isSave = SaveDone(); //let say it return true
    ViewBag.EmailConfirmationSent = true;
    return View("Profile", profileViewModel); // redirect to Index method
}

现在当我使 viewBag 为真时(在 post 方法中),它不应该在索引方法中改变。为了限制我使用了一个全局变量 isSave 但每当它到达索引时,全局变量 "isSave" 总是变为 false 并且我的 viewBag 再次变为 false,这是我不想要的。 请让我知道如何执行此操作。

你不能那样使用,如果你想在两个请求之间保留数据,请改用 TempData