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 6125083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:12:25+00:00 2026-05-23T16:12:25+00:00

How do I make the default modelbinding handle Lists? Let’s say I have a

  • 0

How do I make the default modelbinding handle Lists?

Let’s say I have a ShoppingCart class, which has a ShoppingCartItem List:

Public Class ShoppingCart
  Public Property CouponCode As String
  Public Property Items As New List(Of ShoppingCartItem)
End Class

Public Class ShoppingCartItem
  Public Property Title As String
  Public Property Count As Integer
End Class

My view looks like this (as per the advice from this, and this, blogpost):

@ModelType VAVTag.LuckyDraw.ShoppingCart

@Code
  Layout = Nothing
End Code
<!DOCTYPE html>
<html>
<head runat="server">
  <title>Index</title>
</head>
<body>
  @Using Html.BeginForm()
    @<fieldset>
      <legend>ShoppingCart</legend>
      @Html.TextBoxFor(Function(m) m.CouponCode)
      <hr />
      @Html.EditorFor(Function(m) m.Items)

      <p>
        <input type="submit" value="Save" />
      </p>
    </fieldset>
  End Using
</body>
</html>

The items are rendered using an EditorTemplate (thanks Darin):

@ModelType ShoppingCartItem
@Html.EditorFor(Function(m) m.Title )
@Html.EditorFor(Function(m) m.Count )
<br />

In my controller, I’m inserting some random data, and the view renders my shoppingcart nicely, with three items.

Public Class ShoppingCartController
  Inherits System.Web.Mvc.Controller

  Function Index() As ActionResult
    Dim model As New ShoppingCart
    model.Items.Add(New ShoppingCartItem With {.Count = 1, .Title = "Item A"})
    model.Items.Add(New ShoppingCartItem With {.Count = 17, .Title = "Item B"})
    model.Items.Add(New ShoppingCartItem With {.Count = 100, .Title = "Item C"})

    Return View(model)
  End Function

  <HttpPost()>
  Function Index(model As ShoppingCart) As ActionResult
    Return View(model) ' model is empty!
  End Function
End Class

However, when I submit the page, no values are picked up, not even the CouponCode field. The model object is empty. What gives?

Ultimately, my goal is to add/remove items clientside via javascript and then have the modelbinder pick up the changes automatically when the page is submitted.

Update: I was just missing the Property keyword from my model properties declarations. It’s too late, and I got to get some sleep. 🙂

  • 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-23T16:12:25+00:00Added an answer on May 23, 2026 at 4:12 pm

    How do I make the default modelbinding handle Lists?

    The default model binder already handles lists and dictionaries perfectly fine.

    Just use editor templates. For example:

    View model:

    public class ShoppingCartItem
    {
        public string Title { get; set; }
        public int Count { get; set; }
    }
    
    public class ShoppingCart
    {
        public string CouponCode { get; set; }
        public List<ShoppingCartItem> Items { get; set; }
    }
    

    Controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new ShoppingCart
            {
                Items = new[]
                {
                    new ShoppingCartItem { Count = 1, Title = "Item A" },
                    new ShoppingCartItem { Count = 17, Title = "Item B" },
                    new ShoppingCartItem { Count = 100, Title = "Item C" },
                }.ToList()
            };
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Index(ShoppingCart model)
        {
            return View(model);
        }
    }
    

    View:

    @model ShoppingCart
    @{
        Layout = null;
    }
    <!DOCTYPE html>
    <html>
    <head>
        <title>Index</title>
    </head>
    <body>
        @using (Html.BeginForm())
        {
            <fieldset>           
                <legend>ShoppingCart</legend>
                @Html.TextBoxFor(x => x.CouponCode)
                <hr/>
                @Html.EditorFor(x => x.Items)
            </fieldset>
            <p>
                <input type="submit" value="Save" />
            </p>
        }
    </body>
    </html>
    

    and finally the editor template (~/Views/Home/EditorTemplates/ShoppingCartItem.cshtml) which will be rendered for each item of the Items collection:

    @model ShoppingCartItem
    @Html.EditorFor(x => x.Title)
    @Html.EditorFor(x => x.Count)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model which has a date time field: date = models.DateField(_(Date), default=datetime.now())
Is there a way to make the default access modifier public for variable/method/class declarations?
I have a USER table in database. The table has a RegistrationDate column which
I have a table which has starttime field.this field of type DateTime and I
I have @XmlAttribute(required=true) in hundreds places in a projects. Can I make this default?
How can I make the default border on my ListBoxItems a dotted border? See
Should be a quick and easy question. Does System.Drawing.Graphics.DrawImage() or Image.Save() by default make
Suggest me, is it a good idea to make changes in default Joomla code
My default document is in subfolder not in root how can i make it
How to make android EditText smaller than default in height? If I change the

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.