title: Accessing the value from another class to do the calculation within a controller
body: <p>I'm doing a web application using C# and MVC5. How can I use the value from another class to do the calculation within a controller? For example, I have two classes: Product and Calculation. Product class looks like this:</p>
  
  <pre><code>public class Product
  {
      public int Id { get; set; }
      public string ProductName { get; set; }
      public decimal ProductPrice { get; set; }
  
  public virtual ICollection&lt;Calculation&gt; Calculations { get; set; }       
  }
  </code></pre>
  
  <p>Calculation class looks like this:</p>
  
  <pre><code>public class Calculation
  {
      public int Id { get; set; }
      public int ProductId { get; set; }
      public decimal Quantity { get; set; }
      public decimal TotalPrice { get; set; }
  
      public virtual Product Product { get; set; }
  
  }
  </code></pre>
  
  <p>So, when selecting product from the combobox, I want the method to use price of the selected product (which is property of the class Product) and Quantity(which is property of the class Calculation) to calculate TotalPrice. So, in my CalculationController, method Create looks like this:</p>
  
  <pre><code>// GET: Calculation/Create
      public ActionResult Create(int? id)
      {
          ViewBag.ProductId = new SelectList(db.Products, "Id", "ProductName");
          return View();
      }
  
  // POST: Calculation/Create
      [HttpPost]
      [ValidateAntiForgeryToken]
      public async Task&lt;ActionResult&gt; Create([Bind(Include = "Id,ProductId,Quantity,TotalPrice")] Calculation calculation)
      {
  
  
          if (calculation.TotalPrice == null)
  
          {
  
  // So this part doesn’t work because it can’t access the value of ProductPrice property from another class.
              calculation.TotalPrice = product.ProductPrice * Quantitiy;
  
              db.Calculations.Add(calculation);
              await db.SaveChangesAsync();
              return RedirectToAction("Index");
          }
  
  
          ViewBag.ProductId = new SelectList(db.Products, "Id", "ProductName", calculation.ProductId);
          return View(calculation);
      }
  </code></pre>
  
  <p>I realize I need to initialize it in order to use the value from another class, but can someone please try to explain exactly how to do it on this example, because I'm constantly doing something wrong.</p>
  
  <p>Thank you.</p>