【发布时间】:2021-03-12 20:08:11
【问题描述】:
我学会了如何使用流利的验证器我想知道你是否可以帮助我解决一个问题。我有一个个人系统,在我的控制器中,create post 方法具有以下代码:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create (TicketViewModel model)
{
ICollection <Piece> pieces;
try
{
if (ModelState.IsValid)
{
if (_ticketRepository.GetById (model.Id)! = null)
{
Console.WriteLine ("Ticket already exists!");
}
var _model = _mapper.Map <Ticket> (model);
var count = _ticketRepository.FindAllByPiece (_model);
if (count> 0)
{
ModelState.AddModelError ("", "This ticket already exists for this room. Check the piece, date and time.");
pieces = _pieceRepository.FindAll ();
model.Pieces = pieces;
return View (model);
}
_ticketRepository.Insert (_model);
model.Seats = RegistrationSeats (model.QuantityOfSeats);
return RedirectToAction ("Index");
}
pieces = _pieceRepository.FindAll ();
model.Pieces = pieces;
return View (model);
}
catch (Exception ex)
{
Console.WriteLine (ex.Message);
return View (model);
}
}
请注意,在上面的代码中,我有这样的部分:
var count = _ticketRepository.FindAllByPiece (_model);
if (count> 0)
{
ModelState.AddModelError ("", "This ticket already exists for this room. Check the piece, date and time.");
pieces = _pieceRepository.FindAll ();
model.Pieces = pieces;
return View (model);
}
- 此代码 sn-p 从存储库中调用一项服务,该服务检查通过的模型(票证)是否与银行中已有的模型(票证)具有相同的 partId、日期和时间表。如果存在具有这些相同值的记录,则他不会添加票证,直到该人创建与已存在的票证不同的票证。
- 这是访问银行进行此搜索的服务代码:
public int FindAllByPiece(Ticket model)
{
return _saleTheaterTicketsContext.Tickets.Include(x => x.Piece).Where(x => x.PieceId == model.PieceId && x.Date == model.Date && x.Schedule == model.Schedule).Count();
}
- 这很好,并在视图中显示我在
ModelState.AddModelError添加的消息我确实喜欢在视图中:
<strong class = "text-danger">@ Html.ValidationSummary (true)</strong>
- 我的问题是,当我使用 FluentValidation 时,我想知道是否有任何方法可以做到这一点,这样控制器就不会装满东西。我做了一些研究,但无济于事。我尝试在 Validation 类中实例化存储库(我将在下面放置代码),但我不知道如何对这 3 个字段一起进行验证,将模型传递给存储库。你能帮我吗?
- 这是使用 FluentValidator 的 Validation 类中的代码:
public class TicketViewModelValidator: AbstractValidator <TicketViewModel>
{
public TicketViewModelValidator ()
{
RuleFor (x => x.Price)
.NotEmpty (). WithMessage ("Enter the price")
.GreaterThan (0) .WithMessage ("The price must be greater than 0.00");
RuleFor (x => x.QuantityOfSeats)
.NotEmpty (). WithMessage ("Enter the number of seats")
.GreaterThanOrEqualTo (10) .WithMessage ("The number of seats must be at least 10");
RuleFor (x => x.Date)
.NotEmpty (). WithMessage ("Enter Date")
.Must (ValidDate) .WithMessage ("Date must be greater than or equal to today");
RuleFor (x => x.Schedule)
.NotEmpty (). WithMessage ("Enter time");
RuleFor (x => x.PieceId)
.NotEmpty (). WithMessage ("Select the part");
}
public static bool ValidDate (DateTime date)
{
if (date.Date> = DateTime.Now.Date)
{
return true;
}
return false;
}
}
【问题讨论】:
-
这个问题和LINQ有什么关系?
-
@Thomas Weller e DavidG 大写的标题有什么问题?
-
@NetMage 这个问题与 linq 有关,因为如果您仔细观察,您会发现我在服务上进行的查询使用 linq,但我想在流畅的验证规则中做同样的事情。
-
我不确定 LINQ 是如何参与的,但我要指出的是,您应该在服务器端复制客户端验证,以确保伪造提交无法绕过验证。
-
@NetMage,我上面显示的“FindAllPieces”服务显然使用了 Linq。 Include、Where 等。Fluent Validation 验证双方。
标签: c# linq asp.net-core entity-framework-core fluentvalidation