Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 MVC3从模型中获取所有数据_Asp.net Mvc 3_Razor - Fatal编程技术网

Asp.net mvc 3 MVC3从模型中获取所有数据

Asp.net mvc 3 MVC3从模型中获取所有数据,asp.net-mvc-3,razor,Asp.net Mvc 3,Razor,我制作了一个CRUD MVC应用程序,它有一个叫做的部件,它们之间有一定的关系 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 所有型号都在工作,这方面一切正常。现在,我希望生成

我制作了一个CRUD MVC应用程序,它有一个叫做
的部件,它们之间有一定的关系

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
所有型号都在工作,这方面一切正常。现在,我希望生成一个视图,该视图将显示与所选
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
循环的操作,但我在掌握逻辑方面有困难,有人能帮我吗

谢谢

PartMain
包含Name和Desc属性,也位于o:m
PartSection
和o:m
PartReport
PartSection
包含Name和Desc属性,也位于o:m
PartProperty
中<代码>零件属性
包含名称和说明。
零件报告
包含名称和版本

所以我想做的是使用类似的伪代码。 我在控制器中尝试了这个,用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
    }
}
现在,应该输出类似于我上面提到的内容。当然,这段代码不起作用,但这应该是一般的想法。这就是我请求帮助的原因。谢谢。

控制器:

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)
    });
视图:

@ViewBag.Part.Name
@ViewBag.Part.Desc

@foreach(ViewBag.Sections中的var节) { @section.section.Name @section.section.Desc

@foreach(节.Properties中的var属性) { @属性。名称 @property.Desc } }
my2nd
code
part只是一个显示它应该是什么样子的。我真的无法让它工作:(我会提供更多的代码来让它更清晰。我使用
@{}
将所有内容都移动为内联代码查看,但它工作了。您的代码不工作,因为每次您将值分配给ViewBag.XXX时,您都会丢弃旧值。是的,我找到了它,这就是我将其移动为内联代码的原因。
<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>
}