ASP.NET MVC 3绑定到对象内的集合

时间:2012-02-10 17:22:43

标签: asp.net-mvc asp.net-mvc-3

我的模型包含一个包含这样的集合的对象:

namespace API.Example.Models
{
    public class OrderTest
    {
        public string UserName { get; set; }

        public string Token { get; set; }

        public POCO.Order Order { get; set; }

    }
}

namespace Supertext.API.POCO
{
    public class Order
    {
        public List<TranslationGroup> Groups = new List<TranslationGroup>();
    }


    public class TranslationGroup
    {
        public string GroupId { get; set; }
    }
}

Order对象包含一个名为Groups的集合 在视图中,我显示了这样的集合(使用像几个例子中解释的索引)

@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)

@for (int i = 0; i < Model.Order.Groups.Count; i++)
{ 
    @Html.LabelFor(m => Model.Order.Groups[i].GroupId)
    @Html.TextBoxFor(m => Model.Order.Groups[i].GroupId)
}

这是被调用的Controller方法:

[HttpPost]
public ActionResult Index(Models.OrderTest model)

UserName元素的HTML:

<input id="UserName" name="UserName" style="width:300px;" type="text" value="">

和GroupId元素:

<input id="Order_Groups_0__GroupId" name="Order.Groups[0].GroupId" type="text" value="1">

我可以访问UserName,但集合中没有任何内容 我错过了什么? 还有使用m.UserName和Model.Order.Groups(我的意思是m和Model)之间的区别。这是我的问题吗?

1 个答案:

答案 0 :(得分:2)

POCO实体的每个属性都像CLR管理的属性一样使用。让CLR管理实例的创建等等。或者你可以产生可能引发问题的冲突。

将您的订单代码更改为:

public class Order     
{         
    public List<TranslationGroup> Groups { get; set; }    
}  

- 编辑

我创建了一个新项目并添加了以下类:

public class TranslationGroup
{
    public string GroupId { get; set; } 
}

public class Order
{
    public List<TranslationGroup> Groups { get; set; }
}

public class OrderTest 
{ 
    public string UserName { get; set; } 
    public string Token { get; set; } 
    public Order Order { get; set; } 
} 

这是我的OrderTestController背后的代码:

public ActionResult Index()
{
    var orderTest = new Models.OrderTest()
    {
        UserName = "Vinicius",
        Token = "12as1da58sd558",
        Order = new Models.Order()
        {
            Groups = new List<Models.TranslationGroup>()
            {
                new Models.TranslationGroup() { GroupId = "a54s"},
                new Models.TranslationGroup() { GroupId = "a87d"},
                new Models.TranslationGroup() { GroupId = "2gf4"}
            }
        }
    };


    return View(orderTest);
}

[HttpPost]
public ActionResult Index(Models.OrderTest model)
{
    return View();
}

和索引视图:

@model TestCollection.Models.OrderTest
@{
    ViewBag.Title = "Index";
}
<h2>
    Index</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>OrderTest</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.UserName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.UserName)
            @Html.ValidationMessageFor(model => model.UserName)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Token)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Token)
            @Html.ValidationMessageFor(model => model.Token)
        </div>
        @for (int i = 0; i < Model.Order.Groups.Count; i++)
        {     
            <div class="editor-label">
                @Html.LabelFor(m => Model.Order.Groups[i].GroupId)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => Model.Order.Groups[i].GroupId)
            </div>
        }
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

因此,如果您运行,并转到OrderTest视图,您将看到所有属性已填充,当您单击创建时,所有内容都将被绑定(集合也是如此)。