繁体   English   中英

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

[英]Pass model and modellist data together to a partial view - ASP.NET MVC 5

我正在使用ASP.NET MVC 5应用程序。 我需要在局部视图中创建表单。 我以这种形式传递了ViewModel ,该模型包含所有相关的模型类实例。 现在是模型类之一,我需要传递数据列表,以便可以在剃须刀代码中的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>*@

如果我理解正确,那么您想将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中,然后在视图中检索它。

除了知道您的问题外,“ QualificationViewModel”的数据成员ComponentScheme也不是列表。 因此,您不能使用foreach循环对其进行迭代。

借助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>
                }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM