C# 将模型和模型列表数据一起传递到局部视图-ASP.NET MVC 5
我正在开发一个ASP.NETMVC5应用程序。我需要在局部视图中创建表单。在这个表单中,我传递的是保存所有相关模型类实例的C# 将模型和模型列表数据一起传递到局部视图-ASP.NET MVC 5,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我正在开发一个ASP.NETMVC5应用程序。我需要在局部视图中创建表单。在这个表单中,我传递的是保存所有相关模型类实例的ViewModel。现在,作为一个模型类,我需要传递数据列表,以便在razor代码中打印foreach循环 现在我需要的是传递几个类的模型,并列出一个模型的数据以供查看 非常感谢 视图模型: public class QualificationViewModel : LEO.DAL.ViewModels.IQualificationViewModel { public
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
@*
@DisplayNameFor(model=>model.\u ComponentScheme.ComponentTitle)
标题1
@foreach(模型中的var项目。\u组件方案)
{
@DisplayFor(modelItem=>modelItem.\u ComponentScheme.ComponentTitle)
aaaaaaaaaaaaaa
}
*@
除了了解您的问题之外,“QualificationViewModel”的datamemberComponentScheme
不是一个列表。因此您不能使用foreach循环对其进行迭代。如果我理解正确,您希望将ViewModel发送到部分视图,并且在某些情况下发送另一个列表(ComponentList)相同的观点?如果这是你想要的,你有很多方法:
创建包含两个属性的新视图模型:QualificationViewModel和要发送到视图的类型列表,然后将视图绑定到该新模型
public class ExtendedQualificationViewModel
{
public QualificationViewModel OldViewModel { get; set; }
public IEnumerable<SomeType> ComponenetList {get;set;}
}
或者,您也可以对原始模型进行如下扩展:
public class ExtendedQualificationViewModel : QualificationViewModel
{
public IEnumerable<SomeType> ComponenetList {get;set;}
}
公共类ExtendedQualificationViewModel:QualificationViewModel
{
公共IEnumerable组件列表{get;set;}
}
并在视图中执行相同的绑定
最后,您可以将列表添加到视图数据中,然后在视图中检索它。多亏了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>
}
@if(Model.\u ComponentScheme!=null)
{
@Html.DisplayName(“abvc”)
标题1
@foreach(模型中的var项目。\u组件方案)
{
@DisplayFor(modelitem=>item.ComponentTitle)
aaaaaaaaaaaaaa
}
}
你的问题是什么?如果我理解正确,你想向模型发送对象列表类型限定视图模型
?连同ComponentSchemeEyes的listdata一起,我想发送几个类的列表和模型,你的subscribe方法可以工作…但是我在显示示例ComponentScheme时出错。Title in loop in razor Code如果您可以在这里简单地解释错误以及您实施我的建议的方式,我可能会提供帮助,但是,关闭此问题(通过接受答案)并发布一个新问题(包含新的ViewModel、view和您遇到的错误)可能会更简单。
public class ExtendedQualificationViewModel : QualificationViewModel
{
public IEnumerable<SomeType> ComponenetList {get;set;}
}
@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>
}