我在 InventoryController 中的创建页面因 ASP.NET Core MVC 中的 _Layout 出现错误

问题描述 投票:0回答:1

我正在尝试创建一个项目,但它说

Layout = _DashboardLayout

Dashboard/Inventory/Create.cshtml
中是我的问题,但除了我正在使用
InventoryController
所做的事情之外,我看不到任何问题。

这是错误:

NullReferenceException:未将对象引用设置为对象的实例。
Create.cshtml 中的 AspNetCoreGenerateDocument.Views_Dashboard_Inventory_Create.ExecuteAsync() ...

它在某种程度上与此相关,但有点不同,因为

_DashboardLayout
是问题所在。

刚刚访问模型时ASP.NET Core NullReferenceException

这是我的代码

InventoryController

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Gravitywears.Entities;
using Gravitywears.Models;
using System.Threading.Tasks;

namespace Gravitywears.Controllers.Dashboard
{
    // [Route("Dashboard/[controller]")]
    public class InventoryController : Controller
    {
        private readonly AppDbContext _context;

        public InventoryController(AppDbContext context)
        {
            _context = context;
        }

        // ...

        [HttpPost("Dashboard/Inventory/Create")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Id,ProductName,ProductBrand,Quantity,TypeOfProduct,Price,AgeRange")] Product product)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    _context.Add(product);
                    await _context.SaveChangesAsync();
                    ModelState.Clear();
                    TempData["SuccessMessage"] = "Product successfully created!";
                    return RedirectToAction("Create");
                }
                catch (DbUpdateException)
                {
                    ModelState.AddModelError("", "Please enter a unique inputs.");
                    return View("~/Views/Dashboard/Inventory/Create.cshtml", product);
                }
            }

            // If we got this far, something failed; redisplay form
            TempData["ErrorMessage"] = "Failed to create product. Please check the form and try again.";
            return View("~/Views/Dashboard/Inventory/Create.cshtml", product);
        }
    }
}

这是

Create.cshtml
视图:

@{
    Layout = "_DashboardLayout"; // Use the layout for consistent design
}

@model Gravitywears.Models.Product
                
<form asp-controller="Inventory" asp-action="Create" method="post" class="inv-cp-ppqty">
                    <div class="image">
                        <label for="imageUpload">
                            <svg id="imageLabel" width="16" height="16" viewBox="0 0 16 16" fill="none"
                                xmlns="http://www.w3.org/2000/svg">

                            </svg>
                            <img id="imagePreview"
                                style="display: none; max-width: 100%; height:100%; object-fit: cover; border-radius: 10px;" />
                        </label>
                        @* <input asp-for="Image" type="file" id="imageUpload" name="Image" required
                        onchange="previewImage(event)" /> *@

                        @* <span asp-validation-for="Image" class="text-danger"></span> *@

                        @* <img src="" alt=""> *@
                    </div>
                    <div class="fill-inv">
                        <div class="inp-inv">
                            <input asp-for="ProductName" type="text" placeholder="Product Name" />
                            <span asp-validation-for="ProductName" class="text-danger"></span>

                        </div>
                        <div class="inp-inv">
                            <input asp-for="ProductBrand" type="text" placeholder="Product Brand" />
                            <span asp-validation-for="ProductBrand" class="text-danger"></span>
                        </div>
                        <div class="inp-inv">
                            <input asp-for="Quantity" type="number" placeholder="Quantity" min="1" />
                            <span asp-validation-for="Quantity" class="text-danger"></span>
                        </div>
                        <div class="inp-inv">
                            <select asp-for="TypeOfProduct" id="typeOfProduct" class="form-control">
                                <option value="" disabled selected> Type of Product </option>
                                <option value="Cloth"> Cloth </option>
                                <option value="Pants"> Pants </option>
                                <option value="Accessories"> Accessories </option>
                                <option value="SportsGear"> Sports Gear </option>
                                <!-- Add more options as needed -->
                            </select>
                            <span asp-validation-for="TypeOfProduct" class="text-danger"></span>

                        </div>
                        <div class="inp-inv">
                            <select asp-for="AgeRange" id="ageRange" class="form-control">
                                <option value="" disabled selected> Age </option>
                                <option value="Kids"> -18 </option>
                                <option value="Adults"> 18+ </option>
                                <!-- Add more options as needed -->
                            </select>
                            <span asp-validation-for="AgeRange" class="text-danger"></span>

                        </div>
                        <button type="submit" class="button-type">
                            <h5> Create Item </h5>
                        </button>
                    </div>
</form>

我只期望创建将通过

asp-for
asp-validation-for
和其他属性。现在它也会传递到我的数据库。

但是由于错误与布局有关,但我现在不知道该怎么办。

c# asp.net-core asp.net-core-mvc
1个回答
0
投票

参见圈出的代码...这里似乎您正在重定向到

Create
,但您没有传递模型。

您应该考虑在

if (ModelState.IsValid)
处放置一个断点并从那里进行调试。
product
也可能是
null

调试是你的朋友。

No Model

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.