【问题标题】:Binding to an object with properties and a list绑定到具有属性和列表的对象
【发布时间】:2011-07-07 19:53:14
【问题描述】:

我已经能够正确绑定对象列表。工作正常。现在,当我将项目更改为复杂对象时,它会停止工作。

复杂对象是带有对象列表的房间名称。当“回发”时,名称返回正常,但对象列表返回为空。

有什么提示吗?

房间模型:

    public class Room
{
    public string Name { get; set; }
    public List<Option> Options { get; set; }

    public Room() { }
    public Room(string name, List<Option> options)
    {
        Name = name; Options = options;
    }
}

期权模型

public class Option
{
    public bool IsSelected { get; set; }
    public string ImagePath { get; set; }
    public int UniqueID { get; set; }

    public Option() { }
    public Option(bool isSelected, string imagePath, int uniqueID)
    { IsSelected = isSelected; ImagePath = imagePath; UniqueID = uniqueID; }
}

家庭控制器

    public ActionResult Index()
    {
        List<Option> options = new List<Option>();

        options.Add(new Option(true, "../Content/cars_2.jpg", 4));
        options.Add(new Option(true, "../Content/vw_one_liter_concept01_2.jpg", 6));
        options.Add(new Option(false, "../Content/00018578.jpg", 8));
        //Get a list of selected options and union with all remaining

        Room model = new Room("Room1", options);

        return View(model);
    }
    [HttpPost]
    public ActionResult Index(Room model)
    {
        ViewData["results"] = model.Options.Count();
        return View(model);
    }

索引视图

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MultiSelect.Models.Room>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Index</title>
    <script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery-ui-1.8.10.custom.min.js" type="text/javascript"></script>


</head>
<body>

<% using (Html.BeginForm())
   {%>
    <%= Html.ValidationSummary(true)%>
    <%= Html.TextBoxFor(m=> m.Name) %>
    <% Html.RenderPartial("MultiSelect", Model.Options); %>

<% } %>
</body>
</html>

多选局部视图

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IList<MultiSelect.Models.Option>>" %>
<% for (int counter = 0;counter< Model.Count(); counter ++)
   { %>
    <div class="opt">
        <%= Html.HiddenFor(i=> i[counter].UniqueID)%>
        <%= Html.HiddenFor(i=> i[counter].ImagePath) %>
        <%= Html.CheckBoxFor(i => i[counter].IsSelected)%>
        <img src="<%= Model.ElementAt(counter).ImagePath %>" alt="Image" width="128" height="128" />
    </div>
<% } %>
<input id="Submit1" type="submit" value="submit" />

【问题讨论】:

    标签: model-view-controller list binding properties


    【解决方案1】:

    http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/ 为您提供解决方案。

    另外,一个更简单但不太灵活的解决方案是将 Room.cshtml 文件和 Option.cshtml 文件放在 Shared/EditorTemplates 文件夹中。然后你会放

    <%= Html.TextBoxFor(m=> m.Name) %>
    <% Html.EditorFor(m => m.Options); %>
    

    在 Room.cshtml 中

    <% Html.EditorFor(m => Model); %>
    

    在索引中,以及在 Option.cshtml 中的局部视图的内容。

    【讨论】:

    • 感谢您的回答。 Steven Sanderson 链接向我展示了如何绑定到对象列表。我能够做到这一点,但是当我将它更改为一个包含属性和对象列表(父子模式)的对象时,它返回给我属性(父级),但子级为空。
    猜你喜欢
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    • 2021-07-02
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多