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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:39:39+00:00 2026-05-28T14:39:39+00:00

Say you have the following object: public class Address { public String Line1 {

  • 0

Say you have the following object:

public class Address
{
  public String Line1 { get; set; }
  public String Line2 { get; set; }
  public String City { get; set; }
  public String State { get; set; }
  public String ZipCode { get; set; }

  public Address()
  {
  }
}

public class Contact
{
  public String FirstName { get; set; }
  public String LastName { get; set; }
  public String Telephone { get; set; }

  public Address BillingAddress { get; set; }
  public List<Address> ShippingAddresses { get; set; }

  public Contact()
  {
    // Assume anything that _could_ be null wouldn't be. I'm excluding
    // most "typical" error checking just to keep the examples simple
    this.BillingAddress = new Address();
    this.ShippingAddresses = new List<Address>();
  }
}

Assume the properties are decorated with [Required], [Display] and other attributes.

Then my controller (simplified for the sake of demo):

public ActionResult Edit(String id)
{
  Contact contact = ContactManager.FindByID(id);
  return View(model: contact);
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Contact contact)
{
  if (ModelState.IsValid) //always fails
  {
    ContactManager.Save(contact);
    return RedirectToAction("Saved");
  }
  return View(model: contact);
}

I keep seeing demos on editing an object like this in MVC, but they are constantly breaking out collections of an object in to their own form (e.g. Edit the contact, then edit a specific address off of a contact). I, on the other hand, am trying to editing all this information within the same page and am having no success. For example:

@model Contact

// simplified for brevity
@using (Html.BeginForm())
{
  @Html.LabelFor(x => x.FirstName): @Html.EditorFor(x => x.FirstName)
  @Html.LabelFor(x => x.LastName): @Html.EditorFor(x => x.LastName)
  @Html.LabelFor(x => x.Telephone): @Html.EditorFor(x => x.Telephone)

  <div>
    @Html.LabelFor(x => x.BillingAddress.Line1): @Html.EditorFor(x => x.BillingAddress.Line1)
    @Html.LabelFor(x => x.BillingAddress.Line2): @Html.EditorFor(x => x.BillingAddress.Line2)
    @Html.LabelFor(x => x.BillingAddress.City): @Html.EditorFor(x => x.BillingAddress.City)
    @Html.LabelFor(x => x.BillingAddress.State): @Html.EditorFor(x => x.BillingAddress.State)
    @Html.LabelFor(x => x.BillingAddress.ZipCode): @Html.EditorFor(x => x.BillingAddress.ZipCode)
  </div>

  <div>
  @foreach (Address addr in Model.ShippingAddresses)
  {
    <div>
      @Html.LabelFor(x => addr.Line1): @Html.EditorFor(x => addr.Line1)
      @Html.LabelFor(x => addr.Line2): @Html.EditorFor(x => addr.Line2)
      @Html.LabelFor(x => addr.City): @Html.EditorFor(x => addr.City)
      @Html.LabelFor(x => addr.State): @Html.EditorFor(x => addr.State)
      @Html.LabelFor(x => addr.ZipCode): @Html.EditorFor(x => addr.ZipCode)
    </div>
  }
  </div>
}

The problem I keep running in to is that the ModelState.IsValid never passes when I go to save the information back. Is there a trick to doing this, or is it just outside the realm of MVC? I would like to take an object like Contact and dump all the information on one page for editing, and have it re-save successfully, but I can’t seem to get it to work. (My next step is to tie in ajax so you could dynamically add/remove “ShipingAddresses” on that page, but I need the save to work first–K.I.S.S)

Problems:

  • ModelState.IsValid is almost always false
  • Form elements for collection items frequently have the same names, so in this demo every Line1 in the ShippingAddresses collection dumps to the page as name="addr_Line1" instead of something like ShippingAddresses[0]_Line1 like I’d expect.
  • 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-28T14:39:40+00:00Added an answer on May 28, 2026 at 2:39 pm

    I guess ModelState.IsValid is false because you didn’t populate the shipping addresses collection properly. This happened because you didn’t use proper names for your input fields. Take a look at the following article to understand better what format does the model binder expects your input fields to be named in order to be able to reconstruct values back.

    The reason your code is not generating correct input names is because you used this foreach loop in which the view lost track of the navigation context.

    So try like this:

    @for (var i = 0; i < Model.ShippingAddresses.Count; i++)
    {
        <div>
            @Html.LabelFor(x => x.ShippingAddresses[i].Line1): 
            @Html.EditorFor(x => x.ShippingAddresses[i].Line1)
    
            @Html.LabelFor(x => x.ShippingAddresses[i].Line2): 
            @Html.EditorFor(x => x.ShippingAddresses[i].Line2)
    
            @Html.LabelFor(x => x.ShippingAddresses[i].City): 
            @Html.EditorFor(x => x.ShippingAddresses[i].City)
    
            @Html.LabelFor(x => x.ShippingAddresses[i].State): 
            @Html.EditorFor(x => x.ShippingAddresses[i].State)
    
            @Html.LabelFor(x => x.ShippingAddresses[i].ZipCode): 
            @Html.EditorFor(x => x.ShippingAddresses[i].ZipCode)
        </div>    
    }
    

    Remark: notice that I have also replaced your duplicate labels with an EditorFor call to effectively generate input fields for those fields.

    or even better using editor templates like this:

    @model Contact
    
    @using (Html.BeginForm())
    {
        @Html.LabelFor(x => x.FirstName): 
        @Html.EditorFor(x => x.FirstName)
    
        @Html.LabelFor(x => x.LastName): 
        @Html.EditorFor(x => x.LastName)
    
        @Html.LabelFor(x => x.Telephone): 
        @Html.EditorFor(x => x.Telephone)
    
        @Html.EditorFor(x => x.BillingAddress)
    
        @Html.EditorFor(x => x.ShippingAddresses)
    }
    

    and then define a custom editor template which will automatically be rendered for each element of the ShippingAddresses collection (~/Views/Shared/EditorTemplates/Address.cshtml):

    @model Address
    <div>
        @Html.LabelFor(x => x.Line1): 
        @Html.EditorFor(x => x.Line1)
    
        @Html.LabelFor(x => x.Line2): 
        @Html.EditorFor(x => x.Line2)
    
        @Html.LabelFor(x => x.City): 
        @Html.EditorFor(x => x.City)
    
        @Html.LabelFor(x => x.State): 
        @Html.EditorFor(x => x.State)
    
        @Html.LabelFor(x => x.ZipCode): 
        @Html.EditorFor(x => x.ZipCode)
    </div>
    

    Now you should no longer worry about getting wrong input names. And not only this but you are reusing the address editor template for both your billing address and the collection of shipping addresses. It makes your views DRYier.

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

Sidebar

Related Questions

Say i have the following: public class Person{ public int ID {get; set;} public
ok say i have the following model: public class bar{ public string bar {get;
Let's say I have the following class: public class Test<E> { public boolean sameClassAs(Object
So let's say we have a domain object such as the following public class
Say I have the following class MyComponent : IMyComponent { public MyComponent(int start_at) {...}
Say i have the following two classes: public class User { public int ID
Lets say I have the following code: public class Base { // Some stuff
Say you have the following class public class AccessStatistics { private final int noPages,
Let's say I have the following class definition : List<SomeClass>... public class SomeClass {
Say I have a class, User: public class User { private String name; private

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.