English 中文(简体)
多重模式
原标题:Multiple models into a view

我在此看到这个问题,但似乎与我的情况不同。 我对此可能是错误的,但我们会看到这一点。

Right now I am creating a blog type website in MVC3 (C#) and I can currently create, edit, delete etc a blog just fine and everything works. I am using Code First EF, so I don t know how much that matters.

我有以下简称“博客”模式:

public class BlogPost
{
    public int id { get; set; }
    public string Title { get; set; }
    public DateTime DateCreated { get; set; }
    public ICollection<Topic> Topics { get; set; }
    public string Content { get; set; }
    public ICollection<Comment> Comments { get; set; }
}

(每个博客员额可以有多个专题)

public class Topic
{
    public int id { get; set; }
    public string Name { get; set; }
    public int PostId { get; set; }

    // navigation back to parent
    public BlogPost Post { get; set; }
}

Then there is my DbContext inherited model with all of my models in it:

public class MyModel : DbContext
{
    public DbSet<BlogPost> Posts { get; set; }
    public DbSet<Comment> Comments { get; set; }
    public DbSet<Topic> Topics { get; set; }

    public DbSet<AdminComment> AdminComments { get; set; }
    public DbSet<Bug> Bugs { get; set; }
}

目前,BlogController正在使用假装制造/编辑/提炼/尾料。

private MyModel db = new MyModel();

//
// GET: /Admin/Blog/

public ViewResult Index()
{
    return View(db.Posts.ToList());
}

在另一个模式中,我能做些什么,在名单上也这样说,它将显示与这些员额有关的所有议题,并增加在你目前创建的职位上增加专题的内容?

最佳回答

Create an outer object that has the properties you want your view to be able to see, and use the new object as your model. You have pretty much already done this. Just change your controller to this:

public ViewResult Index()
{
    return View(db);
}

现在,这种观点可以接触到一切。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签