ASP.NET Core:将数据从一张表移动到另一张表

时间:2021-04-22 16:38:26

标签: html asp.net-core

我目前正在使用 ASP.NET Core 开发电子商务网站。我正在尝试使用按钮将数据从 Products 表移动到 Items 表,以便项目表中的数据可以显示在购物篮页面上。任何帮助将不胜感激。

相关按钮正在使用 asp-action=AddToCart

<div class="card-body">
    <h3 class="card-title">@product.Name</h3>
    <svg class="bd-placeholder-img card-img-top" width="100%" >
        <img float="left" width="450" height="450" src="~/assets/Clothing/@product.ProductImage" alt="Image of @product.Name" />
    </svg>
    
    <p class="card-text">@product.Description</p>
    <div class="d-flex justify-content-between align-items-center">
        <div class="btn-group">
            @if (User.IsInRole("Admin"))
            {
                <a class="btn btn-sm btn-outline-secondary" asp-page="/UpdateProductInfo"
                   asp-route-id="@product.Id">Update</a> 
            }
                                
            <a class="btn" asp-action="AddToCart">Add to Cart</a>
            <p class="float-right card-text" >£@product.Price</p>
        </div>
    </div>
</div>

C#

[BindProperty]
public Item Items { get; set; }

public IActionResult addToCart()
{
    _db.Items.Add(Items);
    _db.SaveChanges();
    return Page();
}

product.cs 模型

public class Product
{
    public String Id { get; set; }
    [ForeignKey("Name")]
    public String Name { get; set; }
    public String Description { get; set; }
    [ForeignKey("Price")]
    public int Price { get; set; }
    [ForeignKey("ProductImage")]
    public String ProductImage { get; set; }
    public String ProductImageName { get; set; }
}

Item.cs 模型

public class Item
{
    [Key]
    [ForeignKey("Name")]
    public String Name { get; set; }
    [ForeignKey("Price")]
    public int Price { get; set; }
    [ForeignKey("ProductImage")]
    public String ProductImage { get; set; }

}

2 个答案:

答案 0 :(得分:0)

项目类必须是这样的:

    public class Item
    {
        [Key]
        public int ItemId { get; set; }

        public Product Product { get; set; }

        [ForeignKey("Product")]
        public int ProductId { get; set; }

        public String ProductImage { get; set; }
    }

和您的产品类别:

    public class Product
    {
        [Key]
        public int ProductId { get; set; }

        public string Name{ get; set; }

        public int Price { get; set; }

        public int Description{ get; set; }

        public string ProductImage { get; set; }

        public string ProductImageName { get; set; }

    }

这是两个表之间的(单对单)关系

答案 1 :(得分:0)

<块引用>

我正在尝试将数据从 Products 表移动到 Items 表 使用按钮按下,以便项目表中的数据可以 出现在购物篮页面上。任何帮助将不胜感激。

根据您的描述,我假设在主页面中,您列出了产品,产品的每一行都有一个“添加到购物车”按钮,单击它后,它会将所选产品添加到项目表中,对吗?

由于您的应用程序是Asp.net core Razor Page应用程序,对于“添加到购物车”按钮,您可以使用处理程序方法将路由参数传递给该方法,然后将所选产品添加到Item表中。请参考以下示例:

型号:

public class Product
{
    public String Id { get; set; } 
    public String Name { get; set; }
    public String Description { get; set; } 
    public int Price { get; set; } 
    public String ProductImage { get; set; }
    public String ProductImageName { get; set; }
}
public class Item
{
    [Key] 
    public String Name { get; set; } 
    public int Price { get; set; } 
    public String ProductImage { get; set; }

}

ProductIndex.cshtml:

@page
@model RazorPageSample.Pages.ProductIndexModel
<div class="row">
    <div class="col-md-6">
        <form method="post">
            <table class="table">
                <thead>
                    <tr><th colspan="5">Product Table</th></tr>
                    <tr>
                        <th>
                            Product ID
                        </th>
                        <th>
                            Product Name
                        </th>
                        <th>
                            Description
                        </th>
                        <th>
                            Price
                        </th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var item in Model.Products)
                    {
                        <tr>
                            <td>
                                @item.Id
                            </td>
                            <td>
                                @item.Name
                            </td>
                            <td class="text-right">
                                @item.Description
                            </td>
                            <td class="text-right">
                                £@item.Price
                            </td>
                            <td class="text-right"> 
                                <button asp-page-handler="AddToCart" class="btn btn-primary" asp-route-id="@item.Id">Add to Cart</button>
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
            </form>
    </div>
       <div class="col-md-6">
        <table class="table">
            <thead><tr><th colspan="5">Item Table</th></tr>
                <tr> 
                    <th>
                        Item Name
                    </th>
                    <th>
                        Price
                    </th> 
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @if (Model.Items !=null && Model.Items.Count > 0)
                {
                    foreach (var item in Model.Items)
                    {
                        <tr> 
                            <td>
                                @item.Name
                            </td>
                            <td class="text-right">
                                £@item.Price
                            </td>
                            <td class="text-right"> 
                            </td> 
                        </tr>
                    }
                }
            </tbody>
        </table>
    </div>
</div>

ProductIndex.cshtml.cs:

public class ProductIndexModel : PageModel
{
    //Here I use a DataRepository to set the initial data.
    private readonly IDataRepository _repository;
    public ProductIndexModel(IDataRepository repository)
    {
        _repository = repository;
    }
    [BindProperty]
    public List<Item> Items { get; set; } //item list.
    public List<Product> Products { get; set; } // product list
    public void OnGet()
    {
        Products = _repository.GetProducts();
    }
     
    public async Task<IActionResult> OnPostAddToCartAsync(string id)
    {
        // use session to store the items. 
        // you could directly store the new item into database. in this scenario, you could query the database and get the existing items.
        if (HttpContext.Session.Get<List<Item>>("_Items") != default)
        {
            Items = (List<Item>)HttpContext.Session.Get<List<Item>>("_Items");
        }
        if (id != null)
        {
            var product = _repository.GetProducts().Where(c => c.Id == id).FirstOrDefault();
            Item newitem = new Item()
            {
                Name = product.Name,
                Price = product.Price
            };
            Items.Add(newitem);
            HttpContext.Session.Set<List<Item>>("_Items", Items);
        }
        //reset the Products. 
        Products = _repository.GetProducts();
        return Page();
    }
}

截图如下:

enter image description here

更多详细信息,您可以参考以下链接:

Handler Methods in Razor Pages

Write a basic form

Session and state management in ASP.NET Core

[注意]如果要使用Session来存储Object,在配置好你的应用使用Session之后,记得添加SessionExtensions。