繁体   English   中英

ASP.NET两种模型合而为一

[英]Asp.net two models in one view

我正在尝试让2个模型在1个视图中显示,但是它不起作用。 我尝试了Google提出的许多不同想法,但到目前为止都没有奏效。

错误列表中没有错误。 但是,当我启动它时,会出现此错误。

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]'.

我有一个父类,其中包含to子类。 如果我直接在子类上使用@model IEnumerable <>,它将起作用,但在指向父类时不起作用。

我究竟做错了什么?

编辑

好的,这些是我的文件。

Model1.cs

public int MyProperty1 { get; set; }
public int MyProperty2 { get; set; }

Model2.cs

public int AnotherProperty1 { get; set; }
public int AnotherProperty2 { get; set; }

ViewModel.cs

public IEnumerable<Model1> Model1 { get; set; }
public IEnumerable<Model2> Model2 { get; set; }

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());
}

索引文件

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

现在有了这样的文件,我的错误信息是

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?)

如果您要传递两个且只有两个类,您是否考虑过使用元组?

例如:

在控制器端,

var model = new Tuple<ModelType1, ModelType2>(yourModel1, yourModel2);
return View(model);

在视图的最后,您需要将其与所有可能需要的using语句一起放在顶部:

@model Tuple<ModelType1, ModelType2>

要访问视图中的每个部分, @Model.Item1将是您的ModelType1@Model.Item2将是您的ModelType2

如果结束时使用了两个以上的类,则使ViewModel类具有要包含的各种类型的属性可能是个好主意。 (您也可以复制并向ViewBag添加属性。)

仅使用具有属性的模型类来构成视图所需的两个类,该怎么办?

例如

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

然后在您的视图中使用ViewModel模型。

尝试这个:

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

在您的控制器中:

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);
}

终于我开始工作了。 我不得不更改我的ViewModel,Controller和View。

ViewModel.cs (从IEnumerable到列表)

public List<Model1> Model1 { get; set; }
public List<Model2> Model2 { get; set; }

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 (所以在这里我删除了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.

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