0

我制作了一个 CRUD MVC 应用程序,它有一些Parts相互之间存在某种关系的东西。

PartMain      - o:m Section, o:m Report
PartSection   - m:o Main, o:m Property
PartProperty  - m:o Section, Contains MainID for sorting purposes, SortByMainID
PartReport    - m:o Main

所有模型都在工作,这部分一切正常。现在我希望生成一个视图,它将显示与可以说 selected 相关的所有数据PartMain。像这样:

PartMain: Name, Desc, etc... rest of data
    PartSection1: data...
        PartProperty1: data.. (all properties in relationship with that section)
    PartSection2: data... (all sections in relationship with selected PartMain)
        PartProperty1: data.. (all properties in relationship with that section)
    PartReport: data.... (all reports with selected PartMain)

我知道我应该做一些类似foreach循环的事情,View但我无法掌握逻辑,有人可以帮忙吗?

谢谢。

PartMain包含 Name 和 Desc 属性,也在 o:mPartSection和 o:mPartReport中。PartSection包含 Name 和 Desc 属性,也在 o:mPartProperty中。PartProperty包含名称和描述。PartReport包含名称和版本。

所以我想做的是使用类似的伪代码。 我在控制器中试过这个,用 viewbag 传递数据但什么也没回来

pMainID = selectedMainID;
PartMain partMain = db.PartMain.Find(pMainID);
ViewBag.Name = partMain.Name
ViewBag.Desc = partMain.Desc

var partSections = db.PartSection.Where(s => s.pMainID = pMainID);

foreach (var partSec in partSections)
{
    ViewBag.PartSecName = partSec.Name
    ViewBag.PartSecDesc = partSec.Desc

    var partProperties = db.PartProperties.Where(p => p.pSecID = partSec.ID);
    foreach(var partProp in partProperties)
    {
        ViewBag.PartPropName = partProp.Name
        ViewBag.PartPropDesc = partProp.Desc
    }
}

现在应该输出我上面提到的东西。当然,这段代码不起作用,但这应该是一般的想法。这就是我寻求帮助的原因。谢谢你。

4

1 回答 1

1

控制器:

var pMainID = selectedMainID;
PartMain partMain = db.PartMain.Find(pMainID);

ViewBag.Part = partMain;

ViewBag.Sections = db.PartSection
    .Where(s => s.pMainID = pMainID)
    .Select(s => new
    {
        Section = s,
        Proeprties = db.PartProperties.Where(p => p.pSecID = partSec.ID)
    });

看法:

<h1>@ViewBag.Part.Name</h1>
<p>@ViewBag.Part.Desc</p>

@foreach(var section in ViewBag.Sections)
{
    <h2>@section.Section.Name</h2>
    <p>@section.Section.Desc</p>

    <dl>
    @foreach(var property in section.Properties)
    {
        <dt>@property.Name</dt>
        <dd>@property.Desc</dd>
    }
    </dl>
}
于 2012-08-10T06:58:04.540 回答