[英]ASP.NET Core MVC validation failure
I am new to ASP.NET Core MVC apps.我是 ASP.NET Core MVC 应用程序的新手。 I am trying to create a web app but I am having an issue about validating my model.我正在尝试创建 web 应用程序,但在验证我的 model 时遇到问题。 I have added model code, controller code and razor page.我添加了 model 代码、controller 代码和 razor 页面。 My model validation fails because razor page doesn't start il model.我的 model 验证失败,因为 model 页面无法启动。 normally I don't need iller model.通常我不需要 iller model。
Would you mind anyone can help me about is there any easy solution about validation without custom validation.你介意任何人都可以帮助我关于没有自定义验证的验证是否有任何简单的解决方案。 I am using .NET Core 6.0 and Visual Studio 2022我正在使用 .NET Core 6.0 和 Visual Studio 2022
This is my model class这是我的 model class
using System;
using System.Collections.Generic;
namespace OdevProject.Models
{
public partial class Ilceler
{
public Ilceler()
{
Adreslers = new HashSet<Adresler>();
Bolumlers = new HashSet<Bolumler>();
Doktorlars = new HashSet<Doktorlar>();
Hastanelers = new HashSet<Hastaneler>();
Randevulars = new HashSet<Randevular>();
}
public int IlceId { get; set; }
public int IlId { get; set; }
public string IlceAd { get; set; } = null!;
public virtual Iller Il { get; set; } = null!;
public virtual ICollection<Adresler> Adreslers { get; set; }
public virtual ICollection<Bolumler> Bolumlers { get; set; }
public virtual ICollection<Doktorlar> Doktorlars { get; set; }
public virtual ICollection<Hastaneler> Hastanelers { get; set; }
public virtual ICollection<Randevular> Randevulars { get; set; }
}
}
and this is controller这是 controller
public async Task<IActionResult> Create([Bind("IlceId,IlId,IlceAd")] Ilceler ilceler)
{
//var Il = new Iller { IlId = ilceler.Il.IlId, IlAd=ilceler.Il.IlAd };
if (ModelState.IsValid)
{
var ilce = _context.Ilcelers.Where(x => x.IlceAd == ilceler.IlceAd).FirstOrDefault();
if (ilce != null)
{
ViewBag.Message = "Exist";
return View();
}
else
{
_context.Add(ilceler);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
}
ViewData["IlId"] = new SelectList(await _context.Illers.ToListAsync(), "IlId", "IlAd");
//ViewData["Illist"] = new SelectList(await _context.Illers.ToListAsync(), "IlId", "IlAd");
//ViewData["IlId"] = new SelectList(_context.Illers, "IlId", "IlId", ilceler.IlId);
return View(ilceler);
}
and finally razor pages最后是 razor 页面
@model OdevProject.Models.Ilceler
@{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>Ilceler</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="IlId" class="control-label"></label>
@* @Html.DropDownList("iller",(IEnumerable<SelectListItem>)ViewData["Illist"],"---Il Seciniz---");*@
<select asp-for="IlId" class ="form-control" asp-items="ViewBag.IlId"></select>
</div>
<div class="form-group">
<label asp-for="IlceAd" class="control-label"></label>
<input asp-for="IlceAd" class="form-control" />
@if (ViewBag.Message != null && ViewBag.Message == "Exist")
{
<br /><span style="color:red;">Kayıt mevcut!!!</span>
}
<span asp-validation-for="IlceAd" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
As this document said:正如这份文件所说:
Beginning with .NET 6, new projects include the
<Nullable>enable</Nullable>
element in the project file.从 .NET 6 开始,新项目在项目文件中包含<Nullable>enable</Nullable>
元素。 Once the feature is turned on, existing reference variable declarations become non-nullable reference types .开启该功能后,现有的引用变量声明将变为不可为空的引用类型。
So that the non-nullable property must be required in asp.net 6, otherwise the ModelState will be invalid.所以asp.net 6 中必须要有不可为空的属性,否则ModelState无效。
You can remove <Nullable>enable</Nullable>
from your project file.您可以从项目文件中删除<Nullable>enable</Nullable>
。
Or you can choose to initialize the data like below:或者您可以选择初始化数据,如下所示:
public partial class Ilceler
{
public Ilceler()
{
Il = new Iller();
//...
}
public int IlceId { get; set; }
public int IlId { get; set; }
public string IlceAd { get; set; } = null!;
public virtual Iller Il { get; set; }
//...
}
First of all, thank you for your help.首先,感谢您的帮助。 This is how I solved it.这就是我解决它的方法。 Is it the right approach?这是正确的方法吗? Or is there a more accurate method?或者有没有更准确的方法?
public async Task<IActionResult> Create([Bind("IlceId,IlId,IlceAd")] Ilceler ilceler)
{
Iller Il = new Iller();
Il = _context.Illers.Find(ilceler.IlId);
if (ModelState.IsValid)
{
var ilce = _context.Ilcelers.Where(x => x.IlceAd == ilceler.IlceAd).FirstOrDefault();
if (ilce != null)
{
ViewBag.Message = "Exist";
return View();
}
else
{
_context.Add(ilceler);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
}
ViewData["IlId"] = new SelectList(await _context.Illers.ToListAsync(), "IlId", "IlAd");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.