views:

1891

answers:

1

I have a custom class:

public class Person
{
    public String Name { get; set; }
    public Int32 Age { get; set; }
    public List<String> FavoriteFoods { get; set; }

    public Person()
    {
        this.FavoriteFoods = new List<String>();
        this.FavoriteFoods.Add("Jambalya");
    }
}

I pass this class to my strongly typed view:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcLearner.Models.Person>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
 Create
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Create</h2>

    <% using (Html.BeginForm()) { %>
        <%= Html.LabelFor(person => person.Name) %><br />
        <%= Html.EditorFor(person => person.Name) %><br />
        <%= Html.LabelFor(person => person.Age) %><br />
        <%= Html.EditorFor(person => person.Age) %><br />
        <%= Html.LabelFor(person => person.FavoriteFoods[0]) %><br />
        <%= Html.EditorFor(person => person.FavoriteFoods[0]) %>
    <% } %>

</asp:Content>

And when I browse to the page, I end up with the error message: Templates can be used only with field and property accessor expressions. After a bit of googling, I figured out that this happens because the EditorFor and LabelFor functions can only accept immediate properties of the Model object, and FavoriteFoods[0] is not an immediate property. Does anyone have a work around which would make the code work without using a string to specify the name of the control? Is that even possible? Optimally I would like to use an expression from the model to the item that needs an editor, which determines on its own the name of the control for the input.

Trying some more things, I was able to get this working using the following changes to the view:

<% using (Html.BeginForm()) { %>
    <%= Html.LabelFor(person => person.Name) %><br />
    <%= Html.EditorFor(person => person.Name) %><br />
    <%= Html.LabelFor(person => person.Age) %><br />
    <%= Html.EditorFor(person => person.Age) %><br />
    <% foreach (String FavoriteFoods in Model.FavoriteFoods) { %>
        <%= Html.LabelFor(food => FavoriteFoods) %><br />
        <%= Html.EditorFor(food => FavoriteFoods)%><br />
    <% } %>
    <input type="submit" value="Submit" />
<% } %>

It is important to note however that right side of the expression in EditorFor and LabelFor must be the exact name of the list you are trying to populate and the list must be of a basic type (i.e. String, Int32, etc). When I try to use complex types however, it still fails. I tried to display a list of this person class with inputs for each property and it displays all of them to the browser just fine, but then it returns a null List to my post action. How do I get the index to show up in the name of inputs for items of a list?

This was a giant help in figuring out what was going on.

+2  A: 

I assume this is for the FavoriteFoods list. How it has been done in ASP.NET MVC v1 was having something like this:

<input type="hidden" name="FavouriteFoods.Index" value="0" />
Dan Atkinson
Would you have to rebuild the classes in the controller in MVC 1? I would like for the class to be constructed by the framework if possible and just provide my controller with a packaged up class to work with.
NickLarsen
It does that for you. I think this solution heralds back to Phil Haack's post - http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
Dan Atkinson
Thanks a lot for that link. It looks like its time for me to write some helper methods into my library.
NickLarsen