title: How to return and retrieve object value in C#?
body: <p>I am using 3 layer architecture for my project. But for an instance i need to return Object from a method and retrieve the values.
  Here are my two Models:</p>
  
  <pre><code>public class Product
  {
      public int ProductId { get; set; }
      public string ProductName { get; set; }
      public decimal Price { get; set; }
  }
  
  public class Item
  {
      public Product Products { get; set; }
      public int Quantity { get; set; }
  }
  </code></pre>
  
  <p>and my method is:</p>
  
  <pre><code>public class ProductGateway{
  
  public List&lt;Product&gt; GetProductByProductId(int productId)
      {
          List&lt;Product&gt; productList = new List&lt;Product&gt;();
          SqlConnection connection = new SqlConnection(ConnectionString);
          string query = "SELECT ProductName,cast(Price as decimal(10,2)) as Price FROM   Product  WHERE ProductId='" + productId + "' ";
          SqlCommand command = new SqlCommand(query, connection);
          connection.Open();
          SqlDataReader reader = command.ExecuteReader();
          while (reader.Read())
          {              
              Product product = new Product(); 
              product.ProductName = reader["ProductName"].ToString();
              product.Price = (decimal)reader["Price"];
              productList.Add(product);
          }
          return productList;
  }
      }
  </code></pre>
  
  <p>and my Controller:<strong>CartController</strong></p>
  
  <pre><code>ProductGateway gateway=new ProductGateway();
  public ActionResult BuyProduct( int id)
      {
          if (Session["cart"]==null)
          {
              List&lt;Item&gt; cart= new List&lt;Item&gt;();
              cart.Add(new Item()
              {
                  Products = gateway.GetProductByProductId(id),// this line getting an error(no list)
                  Quantity = 1
              });
              Session["cart"] = cart;
          }
  </code></pre>
  
  <p>And my cshtml view:</p>
  
  <pre><code>@{
                      var cart = (List&lt;Item&gt;) Session["cart"];
                  }
                  @foreach (var item in cart)
                  {
                      &lt;tr&gt;
                          &lt;td class="cart_product"&gt;
                              &lt;a href="@Url.Action("ProductDetails", "Product", new { id = item.Products.ProductId })"&gt;@item.Product.Price &lt;/a&gt;// an error occurred on ProductId and Price   
  </code></pre>
  
  <p>Now problem is that my return type is List the Controller get an error on<code>Products = gateway.AllProductsByProductId(id)</code> and ask for change Item model as <code>Public List&lt;Product&gt; Products</code> .Thus I want to send Object from GetProductByProductId to avoid error in Cart Controller. Is there any way to solve this issue or i need to change entire codes? I really get confused with this scenario.</p>
  
  <p><strong>My Requirement</strong></p>
  
  <p><strong>1.I need to use product property such as <code>@item.Product.Price</code> in my cshtml view. (Current Error: Cannot resolve symbol <code>Price</code>)</strong></p>
  
  <p><strong>2.So that my gateway should return an Product object rather than list of Porduct(for now gateway return list of products).</strong></p>
  
  <p><strong>3. If it is possible to return Object from gateway(assume gateway return single product for each id) then how could I retrieve that object from Cart Controller</strong></p>
  
  <p>Thanks</p>