我有两个表ProductCategory,并将它们链接在一起,然后我就填写了数据.

I want to select the name of the category for add new item and this name is found in the database When I select the name of category from form I can't get the id this name and storage it in the Product table select category name select category name

当我按下保存显示此错误消息时:

error when press save error when press save

在展示产品中无法显示类别的名称:

enter image description here enter image description here

我有这些桌子

using System.ComponentModel;

namespace Project.Models.ProjectClasses
{
    public class Product
    {
        public int ProducId { get; set; }
        public string? Name { get; set; }
        public decimal Price { get; set; }
        public int Quantity { get; set; }
        public int? IDCategory { get; set; }

        public virtual Category? category { get; set; }
        public DateTime? Added_Date { get; set; }=DateTime.Now;
    }

    public class Category
    {
        public int Id { get; set; }
        public string? Name { get; set; }
        public virtual ICollection<Product> ?product { get; set; }
    }
}

这是ProductController:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Project.ECommerceDbContext;
using Project.Migrations;
using Project.Models.ProjectClasses;
using System.Collections.Specialized;
using System.Linq;
using System.Net.Http.Headers;

namespace Project.Controllers
{
    public class ProductsController : Controller
    {
        private readonly ECommerce _CONTEXT;

        public ProductsController(ECommerce CONTEXT)
        {
            _CONTEXT = CONTEXT;
        }

        public IActionResult product()
        {
            IEnumerable<Product> Viewproduct = _CONTEXT.Products.ToList();
            return View(Viewproduct);
        }

        public void Selectecategorywithproduct(int selected = 0)
        {
            List<Category> category = _CONTEXT.Categories.ToList();
            SelectList listItems = new SelectList(category, "Id", "Name", selected);
            ViewBag.SelectedList = listItems;
        }

        public IActionResult Add_Item()
        {
            Selectecategorywithproduct();
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Add_Item(Product Newproduct)
        {
            if (ModelState.IsValid)
            {
                var category = _CONTEXT.Categories.Find(Newproduct.IDCategory);

                Newproduct.IDCategory = category?.Id;
                 if (category?.Id != null) // تحقق من وجود الفئة المحددة
                {
                    Console.WriteLine("error");
                    _CONTEXT.Products.Add(Newproduct);
                    _CONTEXT.SaveChanges();
                    TempData["AddNewProduct"] = $"The product {Newproduct.Name} has been added successfully";
                    return RedirectToAction(nameof(product));
                }
                else
                {
                    ModelState.AddModelError("IDCategory", "Please select a valid category");
                    Selectecategorywithproduct();
                }
                //}
            }

            return View(Newproduct);
        }
    }
}

以下是Product.cshtml个视图:

@using Project.Models.ProjectClasses;

@model IEnumerable<Product>
@{
    ViewData["Title"] = "product";
}

<h1>product</h1>
<div>
    @if (TempData["AddNewProduct"] != null)
    {
        <div id="addProductMessage" class="alert alert-@TempData["AddNewProduct"]" role="alert" style="color:aquamarine">
            @TempData["AddNewProduct"]
        </div>
        <script>
            setTimeout(function () {
                $("#addProductMessage").fadeOut("slow", function () {
                    $(this).remove();
                });
            }, 3000);
        </script>
    }

    @if (TempData["EditProduct"] != null)
    {
        <div id="editProductMessage" class="alert alert-@TempData["EditProduct"]" role="alert" style="color:aquamarine">
            @TempData["EditProduct"]
        </div>
        <script>
            setTimeout(function () {
                $("#editProductMessage").fadeOut("slow", function () {
                    $(this).remove();
                });
            }, 3000);
        </script>
    }

    @if (TempData["DeleteProduct"] != null)
    {
        <div id="deleteProductMessage" class="alert alert-@TempData["DeleteProduct"]" role="alert" style="color:red">
            @TempData["DeleteProduct"]
        </div>
        <script>
            setTimeout(function () {
                $("#deleteProductMessage").fadeOut("slow", function () {
                    $(this).remove();
                });
            }, 3000);
        </script>
    }

    <div class="text-end">
        <a asp-controller="Products" asp-action="Add_Item" class="btn btn-success">Add new item</a>
    </div>
    <table class="table table-hover">
    <thead >
            <tr class="table-primary">
                <th scope="row"></th>
            <th>Product Name</th>
            <th>Product Categroy</th>
            <th>Price</th>
            <th>Quantity</th>
            <th>Added Date</th>
            <th></th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var product in Model)
        {
                <tr class="table-light">
                <td>@product.ProducId</td>
                <td>@product.Name</td>
                    <td>
                        @if (product.IDCategory == null||product.IDCategory==0)
                        {
                            <span>Not select</span>
                        }                        
                        else
                        {
                           @product.category!.Name
                        }
                    </td>

                <td>$ @product.Price</td>
                <td>@product.Quantity</td>
                <td>@product.Added_Date</td>
                    <td>

                        <a asp-controller="Products" asp-action="Edit_Item" asp-route-id="@product.ProducId" class="btn btn-success"><i class="bi bi-pencil"></i> Edit</a>
                        <a asp-controller="Products" asp-action="Delete_Item" asp-route-id="@product.ProducId"  class="btn btn-danger"><i class="bi bi-trash3"></i> Delete</a>
                    </td>
                <td></td>
            </tr>
        }
    </tbody>
</table>

</div>

以下是Add_item.cshtml人的观点:

@using Project.Models.ProjectClasses;
@model Product
@{
    ViewData["Title"] = "Add Item";
}

<h1>Add Item</h1>

<form method="post">
    <fieldset>          
        <div class="form-group">
            <label asp-for="Name" class="form-label mt-4">Product Name</label>
            <input class="form-control" asp-for="Name" placeholder="Enter Product Name">
            <span asp-validation-for="Name" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Price" class="form-label mt-4">Price</label>
            <input class="form-control" asp-for="Price" placeholder="Price">
            <span asp-validation-for="Price" class="text-danger"></span>
        </div>  
         <div class="form-group">
            <label asp-for="Quantity" class="form-label mt-4">Quantity</label>
            <input class="form-control" asp-for="Quantity" placeholder="Quantity">
            <span asp-validation-for="Quantity" class="text-danger"></span>
        </div>  
       <div class="form-group">
           <label asp-for="IDCategory" class="form-label mt-4">Category ID</label>
            <select class="form-control" asp-items="ViewBag.SelectedList">
                <option value="0">Selecte Category</option>

           </select>
            <input type="hidden" id="IDCategoryHidden" name="IDCategoryHidden" value="" />

            <span asp-validation-for="IDCategory" class="text-danger"></span>
       </div>
        <p></p>
        <button type="submit" class="btn btn-primary"><i class="bi bi-check2-all"></i> Save</button>
        <a asp-controller="Products" asp-action="product" class="btn "><i class="bi bi-backspace"></i> Go Back</a>
    </fieldset>
   
</form>
@section Scripts
    {
    <partial name="_ValidationScriptsPartial" />
    
}

拜托,我想解决这个问题

推荐答案

使用下面的代码,这个问题可以被修复.问题的根本原因是您没有正确使用select标签.<select class="form-control" asp-for="IDCategory" asp-items="ViewBag.SelectedList">号房内应该有asp-for=号房.

并且我们还应该使用<input type="hidden" asp-for="IDCategory" value="" />来接收所选值.

    <div class="form-group">
        <label asp-for="IDCategory" class="form-label mt-4">Category ID</label>
        <select class="form-control" asp-for="IDCategory" asp-items="ViewBag.SelectedList">
            <option value="0">Selecte Category</option>

        </select>
        @* <input type="hidden" id="IDCategoryHidden" name="IDCategoryHidden" value="" /> *@
        <input type="hidden" asp-for="IDCategory" value="" />

        <span asp-validation-for="IDCategory" class="text-danger"></span>
    </div>

Test Result

enter image description here

Csharp相关问答推荐

从C#网站调用时找不到存储过程

无法使用并行库并行化我的代码

亚马逊Pinpoint C# SDK

如何使嵌套for-loop更高效?

IComponition.获取IReadOnlyCollection的返回默认属性值

C#无法让WinForm进度条生成动画错误跨线程操作无效

Blazor Web App WASM的两个独立项目令人困惑

是否有必要在ASP.NET Core中注册可传递依赖项?

.NET 8 DI GetServices<;对象&>不工作

Linux Docker上的.NET6在某个时间抛出后,在加密操作期间发生System.Security.Cryptography.CryptographicException:错误

在try 使用访问服务器上的文件夹时,如何解决CORS错误.NET核心API

基于C#和ANGING的SignalR实时聊天流媒体应用

正在从最小API-InvocationConext.Arguments中检索参数的FromBodyAttribute

用于请求用户返回列表的C#Google API

c#在后台实现类型化数组

C#USB条形码 scanner 在第二次扫描时未写入行尾字符

C#;AvaloniaUI;MVVM;当另一个窗口上的按钮被单击时,如何更新视图图像源?

无法使用直接URL通过PictureBox.ImageLocation加载图像

通过mini kube中的远程调试Pod与从emoteProcessPickerScript中解析错误输出的代码错误进行比较

这是T自身的布尔表达式是什么意思?