What is Best Practice for Passing a complex viewmodel from a View to the controller?

Corey Fleig 370 Reputation points
2026-02-04T20:01:17.99+00:00

I have a view form with a complex VM, and it allows the end user to see dozens of data items in various text boxes (as many as 40).

At some point, the user will want to update one or more of the textbox data points, so when the model is modified in the update view, what is the best practice for passing the modified VM content back to the controller?

Right now I'm using HttpContext.Session (get and set). I just don't find it reliable.

Developer technologies | C#
Developer technologies | 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.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Surya Amrutha Vaishnavi Lanka (INFOSYS LIMITED) 1,880 Reputation points Microsoft External Staff
    2026-02-05T12:04:03.31+00:00

    Thanks for reaching out!

    Below are some simple steps which are helpful

    1.  Create a ViewModel: Put all textbox fields inside one class. public class UpdateVM {     public string Name { get; set; }     public string Address { get; set; } }
    2. Bind View to Model: @model UpdateVM
    3. Use BeginForm + TextBoxFor

    using (Html.BeginForm("Update","Home",FormMethod.Post))

    {

        @Html.TextBoxFor(m => m.Name)

        @Html.TextBoxFor(m => m.Address)

     

        <button type="submit">Save</button>

    }

    1. Receive Model in Controller [HttpPost] public IActionResult Update(UpdateVM model) {     // Updated values come automatically here     return View(); }
    2. Avoid Session Do NOT use HttpContext.Session for passing form data. Model binding already handles everything cleanly.

1 additional answer

Sort by: Most helpful
  1. Q&A Assist
    2026-02-04T20:01:26.2066667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To pass a complex view model (VM) back to the controller in ASP.NET MVC, the best practice is to use model binding rather than relying on HttpContext.Session. Here are some recommended steps:

    1. Use Strongly Typed Models: Ensure that your view is strongly typed to the complex view model. This allows the model binder to automatically map the form fields to the properties of the view model when the form is submitted. In your view, you would declare the model at the top:
         @model YourNamespace.YourComplexViewModel
      
    2. Form Submission: Use a form in your view that posts back to the controller. Ensure that the form elements (like text boxes) have names that match the properties of your view model. This way, the model binder can correctly bind the submitted values to the properties of your view model. Example:
         <form asp-action="YourActionName" method="post">
             <input asp-for="Property1" />
             <input asp-for="Property2" />
             <!-- More input fields for other properties -->
             <button type="submit">Submit</button>
         </form>
      
    3. Controller Action: In your controller, create an action method that accepts the complex view model as a parameter. The model binder will automatically populate the view model with the data from the submitted form. Example:
         [HttpPost]
         public IActionResult YourActionName(YourComplexViewModel model)
         {
             if (ModelState.IsValid)
             {
                 // Process the model
             }
             return View(model); // Return the view with the model if needed
         }
      

    Using model binding is more reliable and maintainable than using session state, as it directly maps the form data to your model and allows for better validation and error handling.

    0 comments No comments

Your answer

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