【发布时间】:2021-08-12 11:57:36
【问题描述】:
我是 MVC ASP.NET C# 的新手,所以我无法从 ProductsList 表中获取产品的价格。 销售表有一个总价列,其中 ProductList 中的产品价格乘以用户在销售表中添加的数量,将计算该产品的总价。 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; }
}
}
销售模型类:
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; }
}
}
当我添加新销售时,它会创建 actionresult,新销售将添加到表中:
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:
一旦我点击创建按钮,它应该从销售表中获取数量,从 productlist 表中获取产品价格,然后将它们相乘并显示在 TotalPrice 列下:
创建视图:
@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")
}
【问题讨论】:
标签: c# sql-server asp.net-mvc