Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
786 views
in Technique[技术] by (71.8m points)

asp.net mvc - using Html.EditorFor with an IEnumerable<T>

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...