Hi there fellow coders,
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.
So I have 3 tables with their corresponding attributes:
Presenter
- Name
- MinimumWage
- TaxDeduction
Video
- Title
- Likes
- RatePerLike
Payment
- GrossPay
- Deductions
- NettPay
Presenter has been linked to Payment and Video as a foreign key.
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.
Now my problem is coming in when I'm trying to create a new entry in the Payment table.
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.
Like this:
- GrossPay = (Presenter.MinimumWage + (Video.Likes * Video.RatePerLike)
- Deductions = (GrossPay * (Presenter.TaxDeduction / 100)
- NettPay = (GrossPay - Deductions)
I'm finding it really difficult to find something online to help me with this.
Your help will be greatly appreciated, and if there is any other info you require, please let me know.
Please find code below:
Payment Model
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;
}
}
Video Model
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; }
}
Presenter Model
public partial class Presenter
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Presenter()
{
this.Payments = new HashSet<Payment>();
this.Videos = new HashSet<Video>();
}
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<Payment> Payments { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Video> Videos { get; set; }
}
Payment Controller (Create)
[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);
}
Payment View (Create)
<div class="form-group">
@Html.LabelFor(model => model.PresenterId, "PresenterId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("PresenterId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.PresenterId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
I just can't seem to wrap my head around how to define the parameters accordingly or how to parse data between the models.