首页 > 解决方案 > ASP.NET MVC:将 HTML 表与其他表单数据一起插入数据库

问题描述

我正在尝试开发一个用于产品创建的 ASP.NET MVC 项目。目前,我有以下代码:

模型:

public class ProductModel
{
    public string ProductID { get; set; }
    public string ProductName { get; set; }
    public string ProductDescription { get; set; }
    public string ProductBrandID { get; set; }
    public string ProductBrandName { get; set; }
    public string ProductCategoryID { get; set; }
    public string ProductCategoryName { get; set; }
    public string Inventory { get; set; }
    public string Photos { get; set; }
    public decimal Price { get; set; }
    public decimal Discount { get; set; }
    public decimal Tax { get; set; }
    public List<ProductModel> Products { get; set; }
}

看法

(它有一个表格,其中包含有关产品的一些字段和一个库存表。)

<div class="modal-body" style="padding-left:40px;padding-right:40px">
                <br />
                @using (Html.BeginForm("CreateProduct", "Admin", FormMethod.Post))
                {
                    <form class="Product">
                        <div class="form-group row">
                            <div class="col-sm-7" style="padding-right:20px">

                                <div class="form-group row">
                                    <div class="col-sm-6 mb-3 mb-sm-0">
                                        Product Name: <input type="text" class="form-control" id="txtNewProductname" name="ProductName">
                                    </div>
                                </div>
                                <div class="form-group">
                                    Description: <textarea class="form-control" cols="40" rows="2" id="txtNewProductDescription" name="ProductDescription"></textarea>
                                </div>
                                <div class="form-group row">
                                    <div class="col-sm-6">
                                        Brand: <select id="selectNewProductBrand" class="form-control" name="ProductBrandID">
                                            <option hidden disabled selected> </option>
                                            @foreach (var item in Model.BrandMenus)
                                            {
                                                <option value="@Html.DisplayFor(module => item.BrandID)">@Html.DisplayFor(module => item.BrandName)</option>
                                            }
                                        </select>
                                    </div>
                                    <div class="col-sm-6 mb-3 mb-sm-0">
                                        Category: <select id="selectNewProductCategory" class="form-control" name="ProductCategoryID">
                                            <option hidden disabled selected> </option>
                                            @foreach (var item in Model.Categories)
                                            {
                                                <option value="@Html.DisplayFor(module => item.CategoryID)">@Html.DisplayFor(module => item.CategoryName)</option>
                                            }
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <div class="col-sm-4 mb-3 mb-sm-0">
                                        Price: <input type="text" class="form-control" style="text-align:right;" id="txtNewProductPrice" name="Price">
                                    </div>
                                    <div class="col-sm-4">
                                        Discount: <input type="text" class="form-control" style="text-align:right;" id="txtNewProductDiscount" name="Discount">
                                    </div>
                                    <div class="col-sm-4">
                                        Tax: <input type="text" class="form-control" style="text-align:right;" id="txtNewProductTax" name="Tax">
                                    </div>
                                </div>
                            </div>
                            <div class="form-group col-sm-5" style="border: thin solid lightgray;border-radius: 15px;padding:20px">
                                Photos:
                            </div>
                        </div>
                        <div class="form-group">
                            Inventory:<table class="table table-bordered" id="newTableInventory" width="100%" cellspacing="0">
                                <thead>
                                    <tr>
                                        <th hidden>Inventory ID</th>
                                        <th>Variant</th>
                                        <th>Stock</th>
                                        <th hidden>Location ID</th>
                                        <th>Location</th>
                                        <th>Remarks</th>
                                        <th width="100px">Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td hidden>1000001</td>
                                        <td>Blue</td>
                                        <td>3</td>
                                        <td hidden>123</td>
                                        <td>Shop</td>
                                        <td>Ok</td>
                                        <td></td>
                                    </tr>
                                </tbody>
                            </table>
                            <a href="#" data-toggle="modal" data-target="#CreateNewVariantModal" class="d-none d-sm-inline-block btn btn-sm btn-primary shadow-sm">
                                <i class="fas fa-plus fa-sm text-white-50"></i> Add Variant
                            </a>
                        </div>
                        <div class="modal-footer">
                            <button type="submit" id="btnNewProduct" class="btn btn-primary">Save</button>
                            <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
                        </div>

                    </form>
                }
            </div>

用户界面 在此处输入图像描述

控制器

public class AdminController : Controller
    {
        public ActionResult CreateProduct(ProductModel objModel)
        {
            ProductModelDal cmProducts = new ProductModelDal();
            try
            {
                int result = cmProducts.CreateProduct(objModel);
                if (result == 1)
                {
                    ViewBag.Message = "Product has been added successfully";
                    ModelState.Clear();
                }
                else
                {
                    ViewBag.Message = "Unsucessfull";
                    ModelState.Clear();
                }

                return RedirectToAction("Products");
            }
            catch
            {
                throw;
            }
        }
    }
        
    public class ProductModelDal
    {
        SqlConnection con = new SqlConnection(Properties.Settings.Default.CLDBConString);

        public int CreateProduct(ProductModel productModel)
        {
            SqlCommand cmd = new SqlCommand("sp_Product_Create", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@PRODUCTNAME", productModel.ProductName);
            cmd.Parameters.AddWithValue("@PRODUCTDESCRIPTION", productModel.ProductDescription);
            cmd.Parameters.AddWithValue("@BRANDID", productModel.ProductBrandID);
            cmd.Parameters.AddWithValue("@CATEGORYID", productModel.ProductCategoryID);
            cmd.Parameters.AddWithValue("@PHOTOS", productModel.Photos);
            cmd.Parameters.AddWithValue("@PRICE", productModel.Price);
            cmd.Parameters.AddWithValue("@DISCOUNT", productModel.Discount);
            cmd.Parameters.AddWithValue("@TAX", productModel.Tax);
            con.Open();
            int i = cmd.ExecuteNonQuery();
            con.Close();
            return i;
        }
    }

将产品基本信息插入数据库工作正常。

但是有没有办法同时在表中插入数据呢?

我看到了这篇文章,但我不知道如何将它与我当前的代码集成。请帮我。

先感谢您。

标签: c#htmlasp.net-mvc

解决方案


推荐阅读