title: Simple calculations in an MVC ASP.NET Database First Environment
body: <p>Hi there fellow coders, </p>
  
  <p>I'm pretty new to ASP.Net MVC and I'm finding it difficult to figure out how to do calculations using data from different entities. </p>
  
  <p>So I have 3 tables with their corresponding attributes:</p>
  
  <p><strong>Presenter</strong></p>
  
  <ul>
  <li>Name</li>
  <li>MinimumWage</li>
  <li>TaxDeduction</li>
  </ul>
  
  <p><strong>Video</strong></p>
  
  <ul>
  <li>Title </li>
  <li>Likes</li>
  <li>RatePerLike</li>
  </ul>
  
  <p><strong>Payment</strong></p>
  
  <ul>
  <li>GrossPay</li>
  <li>Deductions</li>
  <li>NettPay</li>
  </ul>
  
  <p>Presenter has been linked to Payment and Video as a foreign key.</p>
  
  <p>So these tables have been created in SQL. The models have been created accordingly in Visual Studio 2015. 
  I've also used scaffolding to create the controllers.</p>
  
  <p>Now my problem is coming in when I'm trying to create a new entry in the Payment table.</p>
  
  <p>Essentially, what needs to happen is all the Payment attributes should be done through calculations using data from the other 2 tables according to which Presenter has been selected. 
  All there is on the Payment Create View is a DropDownList for Presenters and a button to execute the payment for that particular Presenter. </p>
  
  <p>Like this:</p>
  
  <ul>
  <li>GrossPay = (Presenter.MinimumWage + (Video.Likes * Video.RatePerLike)</li>
  <li>Deductions = (GrossPay * (Presenter.TaxDeduction / 100)</li>
  <li>NettPay = (GrossPay - Deductions)</li>
  </ul>
  
  <p>I'm finding it really difficult to find something online to help me with this.</p>
  
  <p>Your help will be greatly appreciated, and if there is any other info you require, please let me know.</p>
  
  <p>Please find code below: </p>
  
  <p><strong>Payment Model</strong></p>
  
  <pre><code>public partial class Payment
  {
      public int PaymentId { get; set; }
      public double GrossPay { get; set; }
      public int PresenterId { get; set; }
  
      public Presenter Presenter { get; set; }
  
      public double calcGrossPay()
      {
          Video v = new Video();
          double gross = 0.0;
  
          gross = (Presenter.MinimumWage + (v.Likes * v.RatePerLike));
  
          return gross;
      }
  
  }
  </code></pre>
  
  <p><strong>Video Model</strong> </p>
  
  <pre><code> public partial class Video
  {
      public int VideoId { get; set; }
      public string Title { get; set; }
      public int PresenterId { get; set; }
      public int Likes { get; set; }
      public double RatePerLike { get; set; }
  
      public virtual Presenter Presenter { get; set; }
  }
  </code></pre>
  
  <p><strong>Presenter Model</strong></p>
  
  <pre><code>public partial class Presenter
  {
      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
      public Presenter()
      {
          this.Payments = new HashSet&lt;Payment&gt;();
          this.Videos = new HashSet&lt;Video&gt;();
      }
  
      public int PresenterId { get; set; }
      public string Name { get; set; }
      public string Contact { get; set; }
      public string Email { get; set; }
      public double MinimumWage { get; set; }
      public double TaxDeduction { get; set; }
  
      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
      public virtual ICollection&lt;Payment&gt; Payments { get; set; }
      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
      public virtual ICollection&lt;Video&gt; Videos { get; set; }
  }
  </code></pre>
  
  <p><strong>Payment Controller (Create)</strong></p>
  
  <pre><code>        [HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Create([Bind(Include = "PaymentId,GrossPay,PresenterId")] Payment payment)
      {
          if (ModelState.IsValid)
          {
              payment.GrossPay = payment.calcGrossPay();
              db.Payments.Add(payment);
              db.SaveChanges();
              return RedirectToAction("Index");
          }
  
          ViewBag.PresenterId = new SelectList(db.Presenters, "PresenterId", "Name", payment.PresenterId);
          return View(payment);
      }
  </code></pre>
  
  <p><strong>Payment View (Create)</strong></p>
  
  <pre><code>&lt;div class="form-group"&gt;
          @Html.LabelFor(model =&gt; model.PresenterId, "PresenterId", htmlAttributes: new { @class = "control-label col-md-2" })
          &lt;div class="col-md-10"&gt;
              @Html.DropDownList("PresenterId", null, htmlAttributes: new { @class = "form-control" })
              @Html.ValidationMessageFor(model =&gt; model.PresenterId, "", new { @class = "text-danger" })
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="form-group"&gt;
          &lt;div class="col-md-offset-2 col-md-10"&gt;
              &lt;input type="submit" value="Create" class="btn btn-default" /&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>I just can't seem to wrap my head around how to define the parameters accordingly or how to parse data between the models. </p>
  
  <p><a href="https://i.stack.imgur.com/Qnkdv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Qnkdv.png" alt="Error"></a></p>