【问题标题】:Asp.net two models in one viewAsp.net 两个模型在一个视图中
【发布时间】:2014-10-11 23:46:04
【问题描述】:

我试图让 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());
}

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

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

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

【问题讨论】:

标签: c# asp.net asp.net-mvc


【解决方案1】:

如果你想要传入两个并且只有两个类,你是否考虑过使用元组?

例如:

在控制器端,

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

在视图端,您会希望它位于顶部,以及您可能需要的任何 using 语句:

@model Tuple<ModelType1, ModelType2>

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

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

【讨论】:

  • 我之前没用过Tuple。我在猜怎么用,还是和以前一样的错误
  • 父类究竟是如何“包含”两个子类的?它们是父类的子类,还是父类本身具有代表子类的属性?
  • 公共类 MainPageModel{ public Model1 Model1{get;集;}公共模型2模型2{get;设置;}}
  • 那么枚举在哪里发挥作用? List&lt;MainPageModel&gt;?
  • @user3147812,您的错误消息与您指出的类名不匹配。请发布模型、用于传递模型的控制器代码和视图
【解决方案2】:

如果只创建一个模型类,其属性构成您的视图所需的两个类?

例如

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 的模型。

【讨论】:

  • 失败是什么意思?
  • 我得到的错误是'Namespace.Models.ViewModel' does not contain a definition for 'Cover' and no extension method 'Cover' accepting a first argument of type 'Namespace.Models.ViewModel' could be found (are you missing a using directive or an assembly reference?)
【解决方案3】:

试试这个:

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

【讨论】:

    【解决方案4】:

    最后我得到了这个工作。我不得不改变我的 ViewModel、Controller 和 View。

    ViewModel.cs (从 IEnumerable 到 List)

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

    【讨论】:

    • 看起来您正在将 2 个模型组合到一个类中,并将该类作为一个模型传递。所以,最后,绑定到您的视图的是单个模型,而不是 2 个模型,不是吗?
    • 基本上是的。因为我需要访问这两个模型,所以我需要将它们放在 Parentmodel 中,因为您只能在视图中包含 1 个模型
    猜你喜欢
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多