Hi @Anjali Agarwal,
The reason why you face the error is you couldn't directly deserialize the object with a iformfilecollection . You could use a custom model and use that custom model to serialize the object.
More details, you could refer to below codes:
1.Create a custom model:
public class FormDataModel
{
public string Title { get; set; }
public string Sections { get; set; }
public string EmployeeName { get; set; }
public string Locations { get; set; }
}
2.Use it inside the controller:
// Create an instance of the custom model and populate it
var formData = new FormDataModel
{
Title = collection["Title"],
Sections = collection["Sections"],
EmployeeName = collection["EmployeeName"],
Locations = collection["Locations"]
};
try
{
TempData["Param"] = JsonConvert.SerializeObject(formData);
//, new { = collection }
return RedirectToAction("Index", "Employee");
}
catch
{
return View();
}
Usage:
public IActionResult Index()
{
var resul2t = TempData["Paramtwo"].ToString();
// Deserialize the JSON back to the custom model
var formData = JsonConvert.DeserializeObject<FormDataModel>(TempData["Param"].ToString());
// Access individual values
var title = formData.Title;
var sections = formData.Sections;
var employeeName = formData.EmployeeName;
var locations = formData.Locations;
return View();
}
Result:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.