Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
public ActionResult PartNumberManufacturer(string id)
        {

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



Then this is my view

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

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

                    <div>

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


                }


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



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

                  </div>
            }




well the above code works fine. but the thing is that if you look back into my ActionResult controller method , i am using :
C#
var partNumber = Context.PartNumberTable.FirstOrDefault(x => x.partNumberId == id);

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:

C#
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>' 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.
Posted
Updated 7-Aug-15 13:10pm
v6

1 solution

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

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


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.
 
Share this answer
 
Comments
1Future 8-Aug-15 18:11pm    
Hi , thank you for responding .... after doing what you said i am no longer getting that error.. but i have another issue that occurring in my project , i don't know where i am going wrong.. in my view i am using for each loop to loop through my model but for some reason it only returns the results of the the first matched record in the database and but doesn't return the other results .. would have any idea to why this is?
kellyapt1 10-Aug-15 15:37pm    
First, put a breakpoint in your controller and make sure that more than one object is coming back from your query.

Second, make sure that you are properly declaring the model object in your view as an IEnumerable.

For example, in my controller I have:

public IActionResult Index()
{
var x = new List<testclass> { new TestClass { PartNumber = "1000" },
new TestClass { PartNumber = "2000" } };
return View(x);
}

And in the view, I have:

@model IEnumerable<webapplication2.models.testclass>

@foreach (var item in Model)
{
<div>@Html.DisplayFor(modelItem => item.PartNumber)</div>

}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900