将模型和模型数据一起传递到局部视图 - ASP.NET MVC 5

时间:2014-03-06 13:03:37

标签: c# asp.net-mvc razor

我正在开发一个ASP.NET MVC 5应用程序。我需要在局部视图中创建表单。在这种形式中,我传递ViewModel,它包含所有相关的模型类实例。现在是模型类之一,我需要传递数据列表,以便我可以在razor代码中的foreach循环中打印。

现在我需要传递几个类的模型并列出一个模型的数据来查看......

非常感谢

查看型号:

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>*@

3 个答案:

答案 0 :(得分:1)

如果我理解正确,您想将ViewModel发送到局部视图,并在某些情况下将另一个列表(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中,然后在视图中检索它。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我已经设法在foreach循环中循环列表数据,感谢JTMon帮助

  @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>
                }