查看与嵌套类对象 [英] View for Object with Nested Classes

查看:105
本文介绍了查看与嵌套类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一些为包含其他对象的引用的对象。

I am trying to create a few for an object that contains references to other objects.

例如,

public class Person
{
    public int ID { get; set; }
    public string Name { get; set; }
    public ICollection<Subscription> Subscriptions { get; set; }
}

public class PersonSubscription
{
    [Key, Column(Order = 1)]
    public int PeopleID { get; set; }
    public Person People { get; set; }

    [Key, Column(Order = 2)]
    public int SubscriptionID { get; set; }
    public Subscription Subscription { get; set; }
}

public class Subscription
{
    public int ID { get; set; }
    public string Magazine { get; set; }
    public ICollection<PersonSubscription> Subscriptions { get; set; }
}

在上面的例子中,当我创建一个新的人,我想允许该人加入到几个新的或现有的订阅。因此,这意味着,不知怎的,认为必须意识到在系统中现有的订阅。此外,在一个相关的说明,当一个订阅补充说,没有previously存在,就需要创建一个新的实例并将其添加到所有相应的数据库。

In the above example, when I am creating a new Person, I want to allow that person to be added to several new or existing subscriptions. So this means that somehow the view needs to be made aware of the existing subscriptions in the system. Also, in a related note, when a subscription is added that did not previously exist, it needs to create a new instance and add it to all of the appropriate databases.

我可能是这个得太多 - 我已经考虑创建一个自定义模型粘合剂(的输入链接的描述在这里),但我不知道是否这将是矫枉过正这一点。

I might be overthinking this - I have considered creating a custom model binder (enter link description here), but I am not sure whether that would be overkill for this.

推荐答案

我不会与模型粘结剂做到这一点。模型绑定是受处理pretty以及基础设施的关注框架的 DefaultModelBinder 。当你需要做一些像绑定到一个模型没有一个无参数的构造函数,或自动填充从请求或HTTP上下文属性使用自定义模型粘合剂。

I wouldn't do this with a model binder. Model binding is an infrastructure concern that is handled pretty well by the framework's DefaultModelBinder. Use custom model binders when you need to do something like bind to a model without a no-arg constructor, or automatically populate properties from the request or http context.

当你说你要一个人被添加到1个或多个订阅,这是在你的系统业务规则,而不是基础设施的关注。因此,与自定义模型粘合剂做这将是模型绑定用户定制的滥用。适当的地方做,这是无论在接受以创建一个新的人,或部分较低层,而不是对MVC管道本身的请求的控制器的动作。

When you say that you want a person to be added to 1 or more subscriptions, this is a business rule in your system, not an infrastructure concern. So doing it with a custom model binder would be a misuse of the model binding customization hook. The appropriate place to do this is either in the controller action that accepts the request to create a new person, or some lower layer, not the MVC pipeline itself.

我不认为你会得到一个非常有用的答案没有的你想虽然完成什么更好的说明。你说你的看法应该有Person.Name一个复选框一个文本框,可能是一个杂志的名单相邻?而另一种可能的文本框中输入一个自定义的杂志?请在你的问题澄清。

I don't think you're going to get a very helpful answer without a better description of what you're trying to accomplish though. Are you saying your view should have a text box for Person.Name, and possibly a list of Magazines with a checkbox next to each? And possibly another text box to enter a custom Magazine? Please clarify in your question.

这篇关于查看与嵌套类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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