[英]Asp.net two models in one view
I'm trying to get 2 models to show in 1 view but it is not working. 我正在尝试让2个模型在1个视图中显示,但是它不起作用。 I have tried lots of different ideas from Google but none have worked so far.
我尝试了Google提出的许多不同想法,但到目前为止都没有奏效。
There is no error in the Error List. 错误列表中没有错误。 But when I start it I get this error.
但是,当我启动它时,会出现此错误。
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Namespace.Models.Class1]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Namespace.Models.ParentClass]'.
I have a parent class containing the to child classes. 我有一个父类,其中包含to子类。 If i use the @model IEnumerable<> directly at the child class it works, but not when pointed at the parent.
如果我直接在子类上使用@model IEnumerable <>,它将起作用,但在指向父类时不起作用。
What am I doing wrong? 我究竟做错了什么?
EDIT 编辑
Ok so these are my files. 好的,这些是我的文件。
Model1.cs Model1.cs
public int MyProperty1 { get; set; }
public int MyProperty2 { get; set; }
Model2.cs Model2.cs
public int AnotherProperty1 { get; set; }
public int AnotherProperty2 { get; set; }
ViewModel.cs ViewModel.cs
public IEnumerable<Model1> Model1 { get; set; }
public IEnumerable<Model2> Model2 { get; set; }
HomeController.cs HomeController.cs
private ConnectContext db = new ConnectContext();
public ActionResult Index()
{
var model = from m in db.model select m;
model = db.model.OrderByDescending(m => m.ID);
return View(db.model.ToList());
}
Index.chstml 索引文件
@model IEnumerable<Namespace.Models.ViewModel>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Model1.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Model2.Title)
</td>
</tr>
}
Now with the files like this my error message is 现在有了这样的文件,我的错误信息是
CS1061: 'System.Collections.Generic.IEnumerable<Namespace.Models.Model1>' does not contain a definition for 'Cover' and no extension method 'Cover' accepting a first argument of type 'System.Collections.Generic.IEnumerable<Namespace.Models.Model1>' could be found (are you missing a using directive or an assembly reference?)
If you have two and only two classes you want to pass in, have you considered using a tuple? 如果您要传递两个且只有两个类,您是否考虑过使用元组?
For example: 例如:
On the controller end, 在控制器端,
var model = new Tuple<ModelType1, ModelType2>(yourModel1, yourModel2);
return View(model);
On the view end, you'll want this at the top, along with any using statements you may need: 在视图的最后,您需要将其与所有可能需要的using语句一起放在顶部:
@model Tuple<ModelType1, ModelType2>
To access each part in the view, @Model.Item1
will be your ModelType1
and @Model.Item2
will be your ModelType2
. 要访问视图中的每个部分,
@Model.Item1
将是您的ModelType1
而@Model.Item2
将是您的ModelType2
。
If you wind up with more than two classes, it might be a good idea for you to make a ViewModel class with properties for the various types you want to include. 如果结束时使用了两个以上的类,则使ViewModel类具有要包含的各种类型的属性可能是个好主意。 (You can also cop out and add properties to the ViewBag.)
(您也可以复制并向ViewBag添加属性。)
What about just making a model class with properties that makes up the two classes you need for your view? 仅使用具有属性的模型类来构成视图所需的两个类,该怎么办?
Eg 例如
public class FirstModel
{
public int Id { get; set; }
public string SomeProperty { get; set; }
}
public class SecondModel
{
public int Id { get; set; }
public string SomeOtherProperty { get; set; }
}
public class ViewModel
{
public FirstModel MyFirstModel { get; set; }
public SecondModel MySecondModel { get; set; }
}
Then in your view you use a model of ViewModel. 然后在您的视图中使用ViewModel模型。
Try this: 尝试这个:
public class FirstModel
{
public int Id { get; set; }
public string SomeProperty { get; set; }
}
public class SecondModel
{
public int Id { get; set; }
public string SomeOtherProperty { get; set; }
}
public class ViewModel
{
public FirstModel MyFirstModel { get; set; }
public SecondModel MySecondModel { get; set; }
}
In your Controller: 在您的控制器中:
private ConnectContext db = new ConnectContext();
public ActionResult Index()
{
FirstModel firstModel = //Set FirstModel Value;
SecondModel secondModel = //Set SecondModel Value;
ViewModel viewModel = new ViewModel(){
FirstModel = firstModel,
SecondModel = secondModel
}
return View(viewModel);
}
Finally I got this working. 终于我开始工作了。 I had to change my ViewModel, Controller and View.
我不得不更改我的ViewModel,Controller和View。
ViewModel.cs (From IEnumerable to List) ViewModel.cs (从IEnumerable到列表)
public List<Model1> Model1 { get; set; }
public List<Model2> Model2 { get; set; }
HomeController.cs HomeController.cs
private ConnectContext db = new ConnectContext();
public ActionResult Index()
{
ViewModel vm = new ViewModel();
vm.Model1 = (from m in db.Model1 select m).OrderByDescending(x => x.ID).Take(3).ToList();
vm.Model2 = (from t in db.Model2 select t).OrderByDescending(x => x.ID).Take(3).ToList();
return View(vm);
}
Index.cshtml (So here I removed the IEnumerable and then each Foreach connects to each Model) Index.cshtml (所以在这里我删除了IEnumerable,然后每个Foreach连接到每个Model)
@model Namespace.Models.ViewModel
@foreach (var item in Model.Model1) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
</tr>
}
@foreach (var item in Model.Model2) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
</tr>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.