使用Html.EditorFor有一个IEnumerable< T> [英] using Html.EditorFor with an IEnumerable<T>

查看:95
本文介绍了使用Html.EditorFor有一个IEnumerable< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义类:

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>

当我浏览的网页,我结束了错误消息:模板只能与字段和属性访问前pressions使用。有点谷歌搜索后,我想通了,出现这种情况是因为EditorFor和LabelFor功能只能接受Model对象直接属性,FavoriteFoods [0]不是立即财产。有没有人有一个变通这将使code的工作,而无需使用一个字符串指定控件的名字吗?是,即使可能吗?最佳我想用一个前pression从模型到需要编辑的项目,这决定了其自身的输入控件的名称

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

但需要注意的是在EditorFor和LabelFor除权pression的右侧必须是您要填充列表的确切名称和清单必须是一个基本类型是很重要的(即字符串,的Int32等)。当我尝试但使用复杂的类型,它仍然失败。我试图显示这个人的类与每个属性的输入列表,它会显示所有的浏览器就好了,但后来它返回一个空列表,以我的职务行为。我如何获得该指数中的投入的名字,以示对列表中的项目?

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?

<一个href=\"http://marcomagdy.word$p$pss.com/2009/09/03/asp-net-mvc-model-binding-form-inputs-to-action-parameters/\"相对=nofollow>这是搞清楚发生了什么巨大的帮助。

推荐答案

我想这是对FavoriteFoods名单。它是如何在ASP.NET MVC V1已经做了有这样的事情:

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

这篇关于使用Html.EditorFor有一个IEnumerable&LT; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆