I have the following line of code in my GET method:
// GET: Areas/Edit/5
public async Task<ActionResult> Edit(int? id)
{
var Str = areas.NOMBRE;
and capture str
in the POST method:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit([Bind(Include = ....)] Areas areas)
{
string StrPost = Str; //str que debe venir del get
I don't know if using a viewbag
(I don't want it to be taken to the View) or what variable to use... I've already had it str
as a global variable but whenever it enters the POST method it deletes the content of str
.
In the case you raise, the most recommended is to use Cookies to store your variable Globally and at the User level .
Still, you can store your variable in
TempData[]
, and you can use it only in the next Request ([HttpPost] Edit()
):As I mentioned, I would use Cookies (better encrypted). In this article you will see how to do it in ASP.NET MVC:
Encrypt and decrypt cookies in an ASP.NET MVC application
UPDATE:
Things to note about
TempData[]
:TempData
can be used to store data between two consecutive requests. The values ofTempData
will be preserved during redirection (Request).TempData
is a guyTempDataDictionary
.TempData
used internallySession
to store the data. So think of it as a short session.TempData
must be typedCast
before use. Checks for null values to avoid runtime errors.TempData
can be used to store only single messages such as error messages, validation messages, or short-term variables.TempData.Keep()
to keep all the values ofTempData
in a third request.