Getting the value of IFormCollection from one controller to another controller

Anjali Agarwal 1,531 Reputation points
2025-01-30T07:23:16.01+00:00

I am getting some values in IFormCollection when I click on the submit button on my Razor page.

      [HttpPost]
      [ValidateAntiForgeryToken]
      public async Task<IActionResult> Index(IFormCollection collection)
      {
          try
          {
              TempData["Param"] = JsonConvert.SerializeObject(collection);
            //, new {  = collection }
              return  RedirectToAction("Index", "ReassignmentReport");
          }
          catch
          {
              return View();
          }
      }

below is the image of the values that I am getting in the controller.

User's image

I am storing the collection in TempData so that I can retrieve the value on other controller where I am redirecting my page to. I am not sure how to retrieve the value on the other controller. I want to retrieve individual value for Title, Sections, EmployeeName and Locations. This is what I have on the other controller where I am redirecting my page to:

    public async Task<IActionResult> Index()
    {
       
        IFormCollection p = JsonConvert.DeserializeObject(TempData["Param"].ToString());   //TempData["Param"].ToString();
        DataTable = await GetReassignData();
        return View(DataTable);
    }

above code does not work, but I want the way to retrieve the values of all four variables that I am passing to this controller.

any help will be appreciated.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,091 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,772 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,277 questions
0 comments No comments
{count} votes

Accepted answer
  1. Brando Zhang-MSFT 3,956 Reputation points Microsoft Vendor
    2025-01-30T08:42:09.8233333+00:00

    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:

    User's image


    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.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.