简体   繁体   English

控制器返回与视图不匹配的值?

[英]Controller returns value that not match with view?

I have two classes, the first one is base class and second one is inherited from first. 我有两个类,第一个是基类,第二个是从第一个继承。

public class hgm
{ 
}

public class Laboratory : hgm
{
}

I used EF Code First to generate the database. 我使用EF Code First来生成数据库。 Also, I used the default Scaffold to generate controllers and views. 此外,我使用默认的Scaffold来生成控制器和视图。

I can use edit, create, details pages but for index(the list of instances), there is an error: 我可以使用编辑,创建,详细信息页面,但索引(实例列表),有一个错误:

The model item passed into the dictionary is of type 'System.Collections.Generic.List'1[armnab.Models.hgm]' , but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable'1[armnab.Models.Laboratory]' 传递到词典中的模型项的类型的'System.Collections.Generic.List'1[armnab.Models.hgm]' ,但是这需要字典类型的模型项'System.Collections.Generic.IEnumerable'1[armnab.Models.Laboratory]'

Here is my controller: 这是我的控制器:

public class LaboratoriesController : Controller
{
    private hgmContext db = new hgmContext();

    // GET: Laboratories
    public ActionResult Index()
    {
        return View(db.hgms.ToList());
    }
}

and the view: 和观点:

@model IEnumerable<armnab.Models.Laboratory>

<h2>Index</h2>

Why is this error occuring? 为什么会出现这个错误?

If Lab inherits from hgm you should either pass a list of laboratories in your controller or change this IEnumerable<armnab.Models.Laboratory> to this IEnumerable<armnab.Models.hgm> . 如果Lab继承自hgm,您应该传递控制器中的实验室列表,或者将此IEnumerable<armnab.Models.Laboratory>更改为此IEnumerable<armnab.Models.hgm>

Unless you typecast it explicitly, while a Laboratory is an hgm an hgm is not necessarily a laboratory so you cannot just assign it. 除非你明确地对它进行类型化,否则实验室是一个hgm hgm并不一定是实验室,所以你不能只是分配它。

Since your DbContext is returning hgm instances and you are looking to populate a view implemented for Laboratories, you need to cast the value to the Laboratory type. 由于您的DbContext正在返回hgm实例,并且您希望填充为Laboratories实现的视图,因此您需要将值转换为实验室类型。 Since you can't guarantee that the DbContext will only return Laboratory instances (given the scope of your question), the Cast operator may throw an exception. 由于您不能保证DbContext只返回实验室实例(给定您的问题的范围), Cast操作符可能会抛出异常。

Given the information in the question, the best choice is OfType which will not return values that aren't the correct type. 根据问题中的信息,最好的选择是OfType ,它不会返回不正确类型的值。

public ActionResult Index()
{
    return View(db.hgms.OfType<Laboratory>());
}

Your view is expecting an IEnumerable of Laboratory. 您的观点期待IEnumerable实验室。 You are returning a 你回来了

List<hgm> 

instead of a 代替

List<Laboratory>

You probably really want to return db.Laboratories.ToList() or if hgm object has Laboraties as a property you need to select the hgm meets criteria and return Laboratories from this eg 您可能真的想要返回db.Laboratories.ToList()或者如果hgm对象将Laboraties作为属性,则需要选择hgm满足条件并从此处返回Laboratories

var result = db.hgm.Where(x => x.someProperty == someValue).ToList();
if(result.Count > 0)
{
   return hgm.Laboratories;
}

There is no inheritance in generic classes ( read this for additional details ) 泛型类中没有继承( 有关其他详细信息,请阅读此内容
If all the objects that you select from DB are laboratory objects, then you can use Cast<Laboratory> linq method. 如果从DB中选择的所有对象都是laboratory对象,则可以使用Cast<Laboratory> linq方法。 If you want all you hgm objects to be treated as laboratory objects than you need to create new laboratory object for each hgm that you get from db and map all the properties (you can use some library for this like AutoMapper or ValueInjecter ) 如果您希望将所有hgm对象视为laboratory对象,则需要为从db获取的每个hgm创建新的laboratory对象并映射所有属性(您可以使用某些库,如AutoMapperValueInjecter

public ActionResult Index()
    {
        return View(db.hgms.Select(x => GetLaboratoryFromHgm(x));
    }

private Laboratory GetLaboratoryFromHgm(Hgm obj) 
{
      var lab = new Laboratory();
      //map all the properties here
}

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

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