如果在视图中使用,则TempData值不会持续存在 [英] TempData value not persisting if used in view

查看:110
本文介绍了如果在视图中使用,则TempData值不会持续存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

TempData["hdn"] = "1";

在控制器中

如果我使用这个

 @{
      var hdn = (string)TempData["hdn"];
  }

在View中为

,在POST中获得null时为TempData["hdn"]值.如果我在查看时跳过此代码,它将保留在POST中.为什么会这样?

in View, TempData["hdn"] value in getting null in POST. If I skip this code in view it persists in POST. Why this is happening?

推荐答案

TempData值在读取后会清除.

TempData values are cleared after they are read.

如果您希望在视图中读取该值后将其返回到控制器中,则需要将其包含在隐藏字段中,然后从表单值中将其读出.

if you want the value back in the controller after you have read it in the view, then you will need to include it in a hidden field and then read it out from the form values.

类似:

<input type="hidden" name="hdn" value="@hdn" />

然后在您的控制器中,您可以执行以下操作:

Then in your controller, you can do:

var hdn = Request.Form["hdn"]

HTH

这篇关于如果在视图中使用,则TempData值不会持续存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆