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

  • Home
  • SEARCH
  • 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 875289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:14:38+00:00 2026-05-15T11:14:38+00:00

I’ve based my work on Phil Haack’s article here: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx I’ve also looked over

  • 0

I’ve based my work on Phil Haack’s article here: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

I’ve also looked over several posts here on StackOverflow and I can’t seem to derive an answer for my problem from them, though, they probably present a solution. I guess I need the bits broken down a bit more.

I’m attempting to get Model Binding working for a view whose Model is a complex type where one of the properties is a List of another complex type. Here are my objects:

        public class Data
        {
            public Data()
            {
                Assessments = new List<AssessmentItem>();
            }

            public String Associate { get; set; }
            public String Department { get; set; }
            public String JobTitle { get; set; }
            public int StartPeriod { get; set; }
            public int EndPeriod { get; set; }
            public List<AssessmentItem> Assessments { get; set; }

        }

        public class AssessmentItem
        {
            public long Index { get; set; }
            public String Heading { get; set; }
            public String Comments { get; set; }
        }   

I’ve attempting to use the EditorFor + control template solution that Haack’s article presents. Here is my Index view (note that it is strongly typed against a single objects and not a List).

Here is the controller method that accepts the post (though, I wouldn’t think that’d matter for the initial view generation):

    [HttpPost]
    public ActionResult Index(Data data)
    {
        return View(data);
    }

As you can see, it doesn’t do anything functional at this stage. It just redisplays the data that was posted.

Here is the editor template in the Shared/EditorTemplates folder.

    (file name: AssessmentItem.ascx location: 
                Views/Shared/EditorTemplates)           
    <%@ Control Language="C#"
    Inherits="System.Web.Mvc.ViewUserControl<Project.Models.AssessmentItem>" %>
    <%@ Import Namespace="Project.Models" %>

    Assessment Item Heading: <%= Model.Heading %>
    <%= Html.HiddenFor(model => Model.Heading) %>
    <%= Html.HiddenFor(model => Model.Index) %>
    <%= Html.TextBoxFor(model => model.Comments) %>
    <%= Html.ValidationMessageFor(model => model.Comments) %>

My confusion is that I’m not sure how to specify the index for each AssessmentItem. Haack’s article & code include several HtmlHelper extensions methods, which I’ve also included in my own code, but that didn’t resolve the issue.

Here is my view’s calling code (this used to use Html.RenderPartial in the loop, but I couldn’t figure out how to bind back to the Model.Assessments list part of the Model):

         <% for (int i = 0; i < Model.Assessments.Count; i++) { %>
            Entering Loop
            <% Html.EditorFor(m => m.Assessments[i] ); %>
        <% } %>

I confess I have a pretty big knowledge gap here. I had only just broken the surface with ASP.NET MVC 1 before attempting ASP.NET MVC 2, though, the improvements seem worth it.

One thing that is very odd is that I can’t seem to use the <%: %> syntax. Any time I do, I get several compile time errors. I can use the typical <% %> & <%= %>, though. I’m pretty stuck here so any comments would be appreciated. The code isn’t throwing any errors. The page loads as if none were thrown as well. It’s just the controls that should be generated via the Html.EditorFor statement aren’t. The ‘Entering Loop’ text is printed twice so I know my list has AssessmentItem objects in it.

Thanks for any and all help.

  • 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-15T11:14:39+00:00Added an answer on May 15, 2026 at 11:14 am

    No need of for loops. All that you need is:

    <%= Html.EditorFor(m => m.Assessments) %>
    

    This will automatically call your AssessmentItem.ascx editor template for each item in the Assessments collection of your model. So just make sure the controller initializes it to some value. It will automatically take care of generating correct names for the inputs in the editor template so that the model binding works.

    <%: ... %> is an ASP.NET 4.0 only and is equivalent to <%= Html.Encode(...) %> but if you are using .NET 3.5 it won’t be available to you. Notice also the absence of ; at the end of the EditorFor helper when used with <%= ... %>.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.