using Html.EditorFor with an IEnumerable<T>

半腔热情 提交于 2019-11-27 06:21:44

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/1478378/using-html-editorfor-with-an-ienumerablet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!