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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:49:20+00:00 2026-05-26T07:49:20+00:00

I am totally at a loss here and can’t find a solution to this

  • 0

I am totally at a loss here and can’t find a solution to this problem. Can somebody please help provide some code to either:

  • Allow [] to be used so as to save contained collection within a @model?
  • Somehow map/bind a contained collection from within a model (I have read Phil Haack’s Collection binding blog but this isn’t a flat collection being passed in….I already have a model A coming in?
  • I have also tried setting up a new view model (contains model classes A and B from below) but my model is coming back null in the httppost (even if I add a simple string type like “Name” and bind it…comes back null). I’m sure there AutoMapper issues that I am not aware of.

I’m a complete newbie with mvc3. Here are the details…

Edit View:

    @model MVC3.Models.A

   // I need to save collection values but can't use [] here to setup model binding.
   // I have read about mapping collections but I already have a model A that is getting passed in.
   //
   @Html.EditorFor(model => model.Bs[0].Val)

Models:

public  class A
{
    public A()
    {
        this.Bs = new HashSet<B>();
    }

    public int Name { get; set; }
    public virtual ICollection<B> Bs { get; set; }  // Can't change this to ILIst because of above HashSet

 - }

       public  class B
       {
           public int Val { get; set; }  
           public virtual A A { get; set; }
       }
  • 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-26T07:49:21+00:00Added an answer on May 26, 2026 at 7:49 am

    Your model has circular references in your view models. That’s not a supported scenario by the default model binder. I would recommend you to always use editor templates in your views. Example:

    Model:

    public  class A
    {
        public int Name { get; set; }
        public virtual ICollection<B> Bs { get; set; }
    }
    
    public class B
    {
        public int Val { get; set; }
    }
    

    Controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new A
            {
                Name = 123,
                Bs = new[]
                {
                    new B { Val = 1 },
                    new B { Val = 2 },
                    new B { Val = 3 },
                }
            };
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Index(A a)
        {
            // The Bs collection will be properly bound here
            return View(a);
        }
    }
    

    View (~/Views/Home/Index.cshtml):

    @model A
    
    @using (Html.BeginForm())
    {
        <div>
            @Html.LabelFor(x => x.Name)
            @Html.EditorFor(x => x.Name)
        </div>
        @Html.EditorFor(x => x.Bs)
        <button type="submit">OK</button>
    }
    

    Corresponding editor template which will be rendered for each element of the Bs collection (~/Views/Home/EditorTemplates/B.cshtml):

    @model B
    <div>
        @Html.LabelFor(x => x.Val)
        @Html.EditorFor(x => x.Val)
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Totally lost with this one. Here's my code: theColor = [NSString stringWithFormat:@white]; NSLog(@%s, theColor);
I've got a problem with ODBC that has me stumped, and I can't find
Please note I'm totally new to ASP.NET.. How can I have the username, password
Please save me from going totally mad here. With my project, I keep finding
I am at a real loss at this problem. I just created a plug-in,
Totally confused about the data types required here. I have this Linq statement: var
Im hoping someone can help me out with a problem Im having with using
Totally out of ideas here, could be needing a simple solution. Basically my desired
Totally confused here. I have a PARENT UIViewController that needs to pass an NSMutableArray
Totally confused by this one... We have a WAMPServer installation set up, running a

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.