ASP.NET MVC 3绑定到一个集合的对象内 [英] ASP.NET MVC 3 Binding to a Collection inside an Object

查看:92
本文介绍了ASP.NET MVC 3绑定到一个集合的对象内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a model with an object that contains a collection like this:

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; }
    }
}

订单对象包含一个名为Groups集合。结果
在视图中显示我这样的集合(与指数像几个例子说明)

The Order object contains a collection called Groups.
In the view I display the collection like this (with the index like explained in several examples)

@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)
}

这是在控制器方法被调用:

And this is the Controller method that gets called:

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

将UserName元素的HTML:

The HTML of the UserName element:

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

和GroupId的元素:

and the GroupId element:

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

我可以访问用户名,但并没有什么收藏。结果
我在想什么?
和最新使用m.UserName和Model.Order.Groups(我的意思是M和模型)之间的差异。那是我的问题?

I can access the UserName, but there is nothing in the collection.
What am I missing? And whats the difference between using m.UserName and Model.Order.Groups (I mean m and Model). Is that my issue?

推荐答案

像由CLR管理的财产POCO实体利用每个属性。让CLR管理创建实例等..或者你可以产生这样你有可能抛出冲突问题。

Each property of POCO entity use like a property managed by the CLR. Let the CLR manage the create of instance and etc.. or you can generate conflicts that can throw issues like you have.

更改订单code这样:

Change your Order code to this:

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

- 编辑

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

I create a new project and add the following class:

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背后C $ C:

Here my code behind of the 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视图,你会看到所有的填充属性,当您单击创建,所有的事情将被绑定(收藏为好)。

So, if you run, and go to OrderTest view, you 'll see all attributes filled, and when you click in create, all things will be binded (the collection as well).

这篇关于ASP.NET MVC 3绑定到一个集合的对象内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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