3

Using the next code in my controller Index action, I try to display 2 fields contents in a razor view:

public class MyController : Controller
{
   private MyEntities db = new MyEntities();
   public ActionResult Index()
      {
         IEnumerable<Prod> signatures = from Prod in db.Products
                                        select new Prod(Prod.ProductID, Prod.ProductName);
      }
}

Previously I have created a class in the models folder and named it "Prod":

public class Prod
{
   public int ProductID { get; set; }
   public string ProductName { get; set; }
}

But when I run the project I fall in the next error:

MyProject.Controllers.MyController.Index()

not all code paths return a value

Any help please ?

Davin Tryon
  • 66,517
  • 15
  • 143
  • 132
Sami-L
  • 5,553
  • 18
  • 59
  • 87

3 Answers3

4

Sami-L, your Prod class does not have specialized constructor, that is, a constructor that accepts some initialization values. Your action should be something like this:

public ActionResult Index()
{
    IEnumerable<Prod> signatures = from p in db.Products
                                    select new Prod
                                    {
                                        ProductId = p.ProductID, 
                                        ProductName = p.ProductName
                                    };
    return View(signatures); 
}

Note the curly braces "{" instead of "(" braces. This should help solve the constructor error which you are getting.

UPDATE: I've renamed a parameter in your LINQ query from Prod to p. Using Prod the way you specified made compiler think that your ProductID and ProductName were static properties. By renaming the parameter after from this should help solve the issue.

Huske
  • 9,186
  • 2
  • 36
  • 53
  • I've tried out your proposal and got the Error **An object reference is required for the non-static field, method, or property 'MyProject.Models.Prod.ProductID.get'** – Sami-L Mar 07 '13 at 14:29
3

I think you are simply missing the return statement.

public ActionResult Index()
{
    IEnumerable<Prod> signatures = from Prod in db.Products
                                        select new Prod(Prod.ProductID, Prod.ProductName);
    return View(signatures);  // this is missing
}
Davin Tryon
  • 66,517
  • 15
  • 143
  • 132
  • @David, Yes of course this is a stupid error, I used **return View(signatures)**; and it worked but then got the next error at the view level: "**Only parameterless constructors and initializers are supported in LINQ to Entities**" The error points at **@foreach( var item in Model )**, please kindly help me to resolve this since I am new to MVC. – Sami-L Mar 07 '13 at 14:09
  • @Sami-L Add a parameterless constructor to your `Product` entity (you can make it protected if you don't want to expose it) – Davin Tryon Mar 07 '13 at 14:10
  • @DavinTryon, return signatures returns an object of type IEnumerable and not an object that inherits from ActionResult. – Huske Mar 07 '13 at 14:12
  • @Husein, so what to do at last ? I have this on the top of my view: **@model IEnumerable** – Sami-L Mar 07 '13 at 14:16
3

You are missing the return element. You will need to wrap this in a View so that your Index razor view will be passed the Prod list.

public ActionResult Index()
{
    IEnumerable<Prod> signatures = from Prod in db.Products
                                   select new Prod(Prod.ProductID, Prod.ProductName);
    return View(signatures);  // add this
}

In the Index.cshtml you can also declare the model so the view is strongly typed, i.e.

@model IEnumerable<MyProject.Models.Prod>

<table>
  @foreach(var prod in Model)
  {
    <tr>
      <td>@prod.ProductID</td>
      <td>@prod.ProductName</td>
    </tr>
  }
</table>
mickfold
  • 2,003
  • 1
  • 14
  • 20