2014-03-06 32 views
0

我工作的一个ASP.NET MVC应用5。我需要在局部视图中创建表单。在这个表格中,我传递了ViewModel,它们包含所有相关的模型类实例。现在是模型类之一,我需要传递数据列表,以便我可以在foreach循环中使用剃须刀代码进行打印。传递模型和modellist数据一起的局部视图 - ASP.NET MVC 5

现在我需要的是通过几个类和一个模型来查看列表数据的模型......

非常感谢

视图模型:

public class QualificationViewModel : LEO.DAL.ViewModels.IQualificationViewModel 
{ 
    public Qualification _Qualification { get; set; } 
    public QualificationType _QualificationType { get; set; } 
    public Subject _Subject { get; set; } 
    public ComponentScheme _ComponentScheme { get; set; } 
} 

控制器:

[HttpGet] 
    public ActionResult CreateNewQualification() 
    { 
     var model = new QualificationViewModel(); 

     var ComponentList = //imagin this is list of components that i need to send along with viewModel    ?????????????????????? 

     return PartialView("PartialQualification_Create", model); 
    } 

查看(需要更正此部分(此处显示清单数据)

@model LEO.DAL.ViewModels.QualificationViewModel 

@*<div class="_FormGrid_block_1"> 
       <table class="table"> 
        <tr> 
         <th> 
          @Html.DisplayNameFor(model => model._ComponentScheme.ComponentTitle) 
         </th>       
         <th>head1</th> 
        </tr> 

        @foreach (var item in Model._ComponentScheme) 
        { 
         <tr> 
          <td> 
           @Html.DisplayFor(modelItem => modelItem._ComponentScheme.ComponentTitle) 
          </td> 

          <td> 
           aaaaaaaaaaaaaa 
          </td> 
         </tr> 
        } 

       </table> 
      </div>*@ 
+2

什么是你的问题? –

+0

如果我理解corectly你想发送一个对象列表类型'QualificationViewModel'到模型? –

+0

与ComponentScheme – toxic

回答

1

如果我理解正确,你要视图模型发送到局部视图,并在某些情况下发送另一个列表(ComponentList),以同样的看法?如果这是你想要的,你有很多方法:

创建一个拥有两个属性的新视图模型:QualificationViewModel,你想发送到视图,然后您的视图绑定到新的模式类型的列表

public class ExtendedQualificationViewModel 
{ 

    public QualificationViewModel OldViewModel { get; set; } 

    public IEnumerable<SomeType> ComponenetList {get;set;} 
} 

,并在视图

@model LEO.DAL.ViewModels.ExtendedQualificationViewModel 

或者你也可以做同样的与原始模型是这样的扩展:

public class ExtendedQualificationViewModel : QualificationViewModel 
{ 
    public IEnumerable<SomeType> ComponenetList {get;set;} 
} 

并在视图中执行相同的绑定。

最后,你可以添加到列表中的ViewData,然后在视图中进行检索。

+0

的共同的ListData是的,我想送togather几类的列表和模式,你的订阅方法的工作......但是我得到错误显示说比如ComponentScheme.Title环路在剃刀代码 – toxic

+0

如果你能解释一下在这里可以很容易地发现错误以及我实施我的建议的方式,但我可能会提供帮助,但是关闭此问题可能更简单(通过接受答案),并使用新的ViewModel,视图和错误发布一个新问题。 – JTMon

+0

非常感谢它的作品 – toxic

0

除了知道您的问题之外,'QualificationViewModel`的数据库ComponentScheme不是列表。所以你不能使用foreach循环迭代它。

0

我已经与感谢JTMon foreach循环设法回路列表数据帮助

@if (Model._ComponentScheme !=null) 
       { 
       <table class="table"> 
        <tr> 
         <th> 
          @Html.DisplayName("abvc") 
         </th>       
         <th>head1</th> 
        </tr> 

        @foreach (var item in Model._ComponentScheme) 
        { 
         <tr> 
          <td> 
           @Html.DisplayFor(modelitem => item.ComponentTitle) 
          </td> 

          <td> 
           aaaaaaaaaaaaaa 
          </td> 
         </tr> 
        } 

       </table> 
       } 
相关问题