ModelState.isValid 返回 false IDK 为什么在 ASP.net MVC 中

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

ModelState.isValid 在 POST 创建操作中返回 false 并在 create.cshtml 中呈现“客户字段是必需的。”

客户.cs

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using WebApplication1.Models;

namespace WebApplication1.Models
{
    public class Customer
    {
        public int CustomerId { get; set; }

        [Required]
        public string Name { get; set; }

        public ICollection<Order> Orders { get; set; }
    }
}

订单.cs

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace WebApplication1.Models
{
    public class Order
    {
        public int OrderId { get; set; }

        [Required]
        public int CustomerId { get; set; }

        // Foreign key relationship to Customer
        [ForeignKey("CustomerId")]
        public Customer Customer { get; set; }
    }
}

创建订单操作

        // GET: Orders/Create
        public IActionResult Create()
        {
            ViewData["CustomerId"] = new SelectList(_context.Customers, "CustomerId", "Name");
            return View();
        }

        // POST: Orders/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("OrderId,CustomerId")] Order order)
        {
            if (ModelState.IsValid)
            {
                _context.Add(order);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }

            ViewData["CustomerId"] = new SelectList(_context.Customers, "CustomerId", "Name", order.CustomerId);
            return View(order);
        }

为订单创建.cshtml

@model WebApplication1.Models.Order

@{
    ViewBag.Title = "Create Order";
}

<h2>Create Order</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <div asp-validation-summary="All"></div>
    <div class="form-horizontal">
        <h4>Order</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @Html.LabelFor(model => model.CustomerId, "Customer", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                
                @Html.DropDownListFor(model => model.CustomerId, (SelectList)ViewData["CustomerId"], "Select a customer", new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.CustomerId, "", 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>
}

我期待 ModelState.isValid 返回 true

asp.net asp.net-mvc model-view-controller
1个回答
0
投票

您可以使用

.NET 6
之外的目标框架。从
.NET 6
开始,必须需要 不可为 null 的属性,否则 ModelState 将无效。

为了达到您的要求,您可以从项目文件中删除

<Nullable>enable</Nullable>

第二种方式,可以添加 ?到您的客户财产:

public class Order
{
    public int OrderId { get; set; }

    [Required]
    public int CustomerId { get; set; }

    // Foreign key relationship to Customer
    [ForeignKey("CustomerId")]
    public Customer? Customer { get; set; }
}
© www.soinside.com 2019 - 2024. All rights reserved.