【问题标题】:LINQ query to get price of product that is added in sales tableLINQ 查询以获取添加到销售表中的产品价格
【发布时间】: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


    【解决方案1】:

    必须输入一个 ID 作为 ProductId。 如果购买包括多个产品,Sales 中应该有一个产品列表。

    这看起来不对: sale.ProductId = TotalPrice;

    【讨论】:

    • 我知道字段名称是由 MVC 生成的,我也可以将其更改为 ProductName,因为我现在不关注字段。下拉菜单包含所有带有产品 ID 的产品名称
    • 你必须设置 Html.BeginForm("Functionname","Controllername")
    • 例如:@Html.BeginForm("Create","Sale") if Controller File 是 SaleController
    • 这不是我回答的问题
    猜你喜欢
    • 1970-01-01
    • 2017-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    • 2019-03-24
    相关资源
    最近更新 更多