I am New to MVC ASP.NET C#, so i am having trouble in getting product' price from ProductsList table. The Sales Table has a column of Total Price where price of product from ProductList is multiplied with the quantity added in Sales Table by user and total price will be calculated for that product. Model Class for ProductList:
namespace InventorySystem.Models
{
using System;
using System.Collections.Generic;
public partial class ProductList
{
public ProductList()
{
this.Inventories = new HashSet<Inventory>();
this.Sales = new HashSet<Sale>();
}
public int ProductId { get; set; }
public string ProductName { get; set; }
public int ProductPrice { get; set; }
public string ProductCategory { get; set; }
}
}
Model Class for Sales:
namespace InventorySystem.Models
{
using System;
using System.Collections.Generic;
public partial class Sale
{
public int SalesId { get; set; }
public int ProductId { get; set; }
public int TotalPrice { get; set; }
public int Prod_Quantity { get; set; }
public System.DateTime Date { get; set; }
public virtual ProductList ProductList { get; set; }
}
}
When i add new sales it will go to create actionresult, where new sale will be added in table:
public ActionResult Create([Bind(Include = "SalesId,ProductId,TotalPrice,Prod_Quantity,Date")] Sale sale)
{
if (ModelState.IsValid)
{
// here with the multiplication i want the product price from products table
int TotalPrice = sale.Prod_Quantity * ;
sale.TotalPrice = TotalPrice;
db.Sales.Add(sale);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.ProductId = new SelectList(db.ProductLists, "ProductId", "ProductName", sale.ProductId);
return View(sale);
}
SS of UI:
As soon as i click on create button it should get the quantity from sales table and price of product from productlist table and multiply them and display under column of TotalPrice:
CREATE VIEW:
@model InventorySystem.Models.Sale
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Sale</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.ProductId, "ProductName", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("ProductId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ProductId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Prod_Quantity, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Prod_Quantity, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Prod_Quantity, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Date, "", 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>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

