简体   繁体   English

ASP.NET MVC复杂模型属性

[英]ASP.NET MVC Complex Model properties

I'm creating a new ASP.NET MVC (with Entity Framework) project and I'm struggling a bit to understand what should I do when facing this "problem": 我正在创建一个新的ASP.NET MVC(带有Entity Framework)项目,并且在理解这个“问题”时我应该做些什么:

Lets say I have the foo model: 可以说我有foo模型:

public class foo
{
    public int ID { get; set; }

    public virtual Book {get; set;}

    public int BookID {get; set; }

    public virtual ICollection<Theme> Themes {get; set;}

    /* Now what ?!? */
}

So let me try to explain: 因此,让我尝试解释一下:

  • I learned that to simply the View-Controller process of getting a dropdownlist to get a select element and binding it with his "parent" model I had to have an "ID" field for the Book ID and a virtual property for the actual relation with the Book model. 我了解到,只要简单地通过一个View-Controller过程来获得一个下拉列表以获取一个select元素并将其与他的“父”模型绑定,我就必须为Book ID具有一个“ ID”字段,并为与书籍模型。 I really don't know if it's the "best" approach for start. 我真的不知道这是否是开始的“最佳”方法。

  • Then I had to add a "list" of other model type to my Foo model. 然后,我必须在Foo模型中添加其他模型类型的“列表”。 So now what should I do? 那么,现在我该怎么办? In terms of View I was thinking creating a list of checkboxes with a scroll with do the trick but my main problem is still figure out how to handle the "model relation problem". 就视图而言,我正在考虑使用滚动创建一个复选框列表,但是我的主要问题仍然是弄清楚如何处理“模型关系问题”。

Thank you. 谢谢。

You need refer foo in Theme 您需要在主题中引用foo

public class foo
{
    public int ID { get; set; }

    public virtual Book {get; set;}

    public int BookID {get; set; }

    public virtual ICollection<Theme> Themes {get; set;}

}

public class Theme
{
    public int ID { get; set; }

    [Foreignkey("fooID")]
    public virtual foo foo{get; set;}

    public int fooID {get; set; }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM