Hi kabiri.ehsan,
To save the object in TempData you need to Serialize it and assign to TempData.
Then when ever required Deserialize it and use.
Refer below example. Add Newtonsoft dll to the preject.
Namespaces
using Newtonsoft.Json;
Controller
public class HomeController : Controller
{
public IActionResult Index()
{
MyObject myObject = new MyObject()
{
Age = 99,
FullName = "MichaelJackson"
};
TempData["Key"] = JsonConvert.SerializeObject(myObject);
return View();
}
[HttpPost]
public IActionResult Save()
{
MyObject myObject2 = JsonConvert.DeserializeObject<MyObject>(TempData["Key"].ToString());
return Content($"Name: { myObject2.FullName} , Age: { myObject2.Age}");
}
public class MyObject
{
public int Age { get; set; }
public string FullName { get; set; }
}
}
View
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form method="post" asp-controller="Home" asp-action="Save">
<input id="btnSubmit" type="submit" value="Submit" />
</form>
</body>
</html>
Screenshot