Zulqadar Idrishi

Zulqadar Idrishi

  • NA
  • 471
  • 71k

Pass complex object on Post Redirection without TempData in .Net Core.

Jun 1 2021 5:27 AM
Currently, I'm working with the following approach and It's working fine but I want to remove the use of TempData. What can I do? 
 
//my class
public myClass{
    public int id {get;set;}
    public string name {get;set;}
    public anotherClass another {get;set;}
 
public anotherClass{
    public int id2 {get;set;}
    public string name2 {get;set;} 
 
//my action method
 
[HttpPost]
public async Task<IActionResult> Search(string searchId)
{
   var ob=new myClass();
   TempData["myClass"] = JsonConvert.SerializeObject(ob);
    return RedirectToAction("DataList"); 
 
 
[HttpGet]
public IActionResult DataList()
{
var val = TempData.Peek("myClass");
var res = JsonConvert.DeserializeObject<myClass>(val.ToString());
return View(res);
}
 

Answers (3)