views:

32

answers:

1

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  A: 

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 <%= ... %>.

Darin Dimitrov
I've spent 8 hours on this darn puzzle today. That worked. Thank you for ending my pain.
Jason