Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6003693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:09:27+00:00 2026-05-23T01:09:27+00:00

When the form in the below view is submitted the cart object in session

  • 0

When the form in the below view is submitted the cart object
in session fails to update with the client side inputed values.
Am I missing something ? Is the custom model binder to blame ?

All the element names are 1:1 with the cart object property names and structure.

View Excerpt:

         <% using (Html.BeginForm("UpdateCart", "Cart"))
       { %>



<% int index = 0; %>

<% foreach (var line in Model.Cart.Lines)
   { %>
<tr>
<%: Html.Hidden("Lines.Index", index) %>
<td align="center"><%: Html.TextBox("Lines[" + index + "].Quantity", line.Quantity)%></td>
<td align="left"><%: line.Product.Name%></td>
<td align="right"><%: line.Product.ListPrice.ToString("c")%></td>
<td align="right">
<%: (line.Quantity * line.Product.ListPrice).ToString("c")%>
</td>
</tr>

<%--<%: Html.Hidden("Lines[" + index + "].Product.ProductID", line.Product.ProductID)%>--%>

<% index++; %>

<% } %>

    <%: Html.Hidden("returnUrl", Model.ReturnUrl) %>


    <input type="submit" value="Update Cart" />

    <% } %>

Cart Object:

    public class Cart
{
  private IList<CartLine> lines = new List<CartLine>();
    public IList<CartLine> Lines { get { return lines; } set { lines = value; } }
}

    public class CartLine
    {
        public Product Product
        {
            get;
            set;
        }

        public int Quantity
        { get; set; }
    }

Action Method that receives posted form data and should update the cart object by binding the form values:

[HttpPost]
    public RedirectToRouteResult UpdateCart(Cart cart, string returnUrl)
    {



         return RedirectToAction("Index", new { returnUrl });

    }

Custom model binder that instantiates cart in session and returns that instance for all controllers and methods to use.

public class CartModelBinder : DefaultModelBinder
{



    private const string cartSessionKey = "_cart";

    protected override void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        controllerContext.HttpContext.Session[cartSessionKey] = (Cart)bindingContext.Model;
    }




        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            //if (bindingContext.Model != null)
            //    throw new InvalidOperationException("Cannot update instances");

            Cart cart = (Cart)controllerContext.HttpContext.Session[cartSessionKey];

            if (cart == null)
            {
                cart = new Cart();
                controllerContext.HttpContext.Session[cartSessionKey] = cart;

            }

            return cart;

        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:09:28+00:00Added an answer on May 23, 2026 at 1:09 am

    The cutom modelbinder you created is called when you post your data to ther server, so your parameter cart of the action UpdateCart will be loaded with the data from client using the modelbinder. To use a custom modelbinder to update your session after the object is loaded you can do this:

    public class CartModelBinder : DefaultModelBinder
    {
        private const string cartSessionKey = "_cart";
    
        protected override void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            controllerContext.HttpContext.Session[cartSessionKey] = bindingContext.Model;
        }
    }
    

    In this modelbinder we are inheriting the defaultmodelbinder and overiding the OnModelUpdate event that happens after the model is binded, so in the OnModelUpdated we have the model with the values from the view and we can set the session.

    But for the DefaultModelBinder do the bind you’ll have to change you view, the names of the inputs must match the names of the properties of your object, so instead of cart.Lines[” + index + “].Quantity you should use Lines[” + index + “].Quantity (do the same for the other properties). In you Cart object you’ll have to create a setter for the Lines property because without that the defaultbinder won’t be able to set the value inputed. You don’t need to call UpdateModel inside your view.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form like below in the view : Here i am not
In my view there is a form shown below <% using (Html.BeginForm(SaveRecords, Search, FormMethod.Post))
In a MVC view, I have a form as below. when user click on
I have the below form in my view: <% form_for(@filedata, :url=>{ :controller=>filedatas, :action=>create}, :multipart
For the form below, how could I make the input field big, like maybe
I'd like to take my form below, and somehow adjust it to submit the
After a user adds 5 emails and their name to form below, and then
Every time I write a program of the form below using LINQ to SQL,
My below mentioned code still submits form on special character in name field. Validation
My form has a custom element like below, created using custom ajax: <select jwcid=testtest

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.