模型不包含定义错误 [英] Model does not contain a definition error

查看:94
本文介绍了模型不包含定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部好,



我在项目中遇到问题,并想知道是否有人可以指出我可能出错的地方。所以基本上在我的项目中我有一个像这样的ActionResult方法



HI All,

I am having an issue in my project and wonder if some can pinpoint me to where i may have gone wrong. so basically in my project i have got an ActionResult method like so

public ActionResult PartNumberManufacturer(string id)
        {

         var partNumber = _Context.PartNumberTable.FirstOrDefault(x => x.partNumberId == id);
            
           return PartialView("PartNumberView",    partNumber);
}







这是我的观点






Then this is my view

@if (Model != null && !string.IsNullOrEmpty(Model.PartNumberManufacturer))
{

                @if (Model.PartNumberManufacturer == "Fr")
                {

                    <div>

                        <p> This product is developed in france </p>
                    </div>


                }







@if (@Model.PartNumberManufacturer == "Ger")
            {



                    <p> This Product is developed in Germany </p>

                  </div>
            }









以上代码工作正常。但问题是,如果你回顾我的ActionResult控制器方法,我正在使用:





well the above code works fine. but the thing is that if you look back into my ActionResult controller method , i am using :

var partNumber = Context.PartNumberTable.FirstOrDefault(x => x.partNumberId == id);



FirstOrDefault-course只返回数据库中第一个找到的零件号。

这不是我想要的东西我不希望它只返回第一个找到的记录,我希望它返回数据库中搜索查询匹配的所有记录。所以为了做到这一点并在网上进行了一些研究之后,我发现我必须将结果返回到列表中,这使我修改了我的ActionResult方法,如下所示:




FirstOrDefault of-course will only return the first found part number in the database.
This isnot what i am looking for i don't want it only to return the first found record , i want it to return all the records that match by the search query in the database. So in order to do and after a few research online i found that i had to return the results into a list and this made me amend my ActionResult method to like this:

public ActionResult PartNumberManufacturer(string id)
        {
 
 var _methodOfRepair = Context.PartNumberTable.Where(x => x.PartNumberId == id); // changed this line and this is where i get the error message saying value cannot be Null
            
           return PartialView("PartNumberView",    partNumber);
}





所以是的,我收到错误消息,说



so yeah i get the error message saying that

Quote:

{'System.Data.Entity.Infrastructure.DbQuery< myproject.model.partnumbertable>'不包含'PartNumberManufacturer'的定义}

{"'System.Data.Entity.Infrastructure.DbQuery<myproject.model.partnumbertable>' does not contain a definition for 'PartNumberManufacturer'"}

我做了这些更改。我在这做错了什么?或者是否有一种不同的方法我可以推荐这样做?



谢谢您的时间。

i made those changes. what am i doing wrong here? or is there a different approach i can be recommended to in order to achieve this?

thank you for your time.

推荐答案

我认为您需要在查询中添加ToList():



I think that you need to add a ToList() to your query:

Context.PartNumberTable.Where(x => x.PartNumberId == id).ToList();





如果没有ToList(),你试图返回一个DbQuery对象到您的视图,而不是具有PartNumberManufacturer属性的对象列表。



此外,在您的视图中,您必须将@Model视为列表或IEnumberable并迭代它。



Without the ToList() you are trying to return a DbQuery object to your view, not a list of objects with a PartNumberManufacturer property.

Also, in your view you will have to treat your @Model as a list or IEnumberable and iterate over it.


这篇关于模型不包含定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆