x.Cont-6ren">
gpt4 book ai didi

c# - 外键的 FluentValidation RuleFor 属性

转载 作者:太空宇宙 更新时间:2023-11-03 15:25:14 25 4
gpt4 key购买 nike

当我运行该程序时,我在 x.Album.Title == "Disintegration" 中收到 System.NullReferenceException:

RuleFor(x => x.Contents)
.NotNull()
.When(x => x.Album.Title == "Disintegration");

我如何对此进行编程,以便 Album.Title == "DisintegrationContents 不被接受为 Null?

模型

[Validator(typeof(ReviewValidation))]
public class Review
{
public int ReviewID { get; set; }

[Display(Name = "Album")]
public int AlbumID { get; set; }

public virtual Album Album { get; set; }

public string Contents { get; set; }

[DataType(DataType.EmailAddress)]
public string ReviewerEmail { get; set; }
}

验证

public class ReviewValidation : AbstractValidator<Review>
{
public ReviewValidation()
{
RuleFor(x => x.Contents)
.NotNull()
.When(x => x.Album.Title == "Disintegration");
}
}

Controller

public class ReviewsController : Controller
{
private StoreContext db = new StoreContext();
public ActionResult Create()
{
ViewBag.AlbumID = new SelectList(db.Albums, "AlbumID", "Title");
return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Review review)
{
if (ModelState.IsValid)
{
db.Reviews.Add(review);
db.SaveChanges();
return RedirectToAction("Index");
}

ViewBag.AlbumID = new SelectList(db.Albums, "AlbumID", "Title", review.AlbumID);
return View(review);
}
}

查看

@model MVCMusicStore.Models.Review


@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>Review</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.AlbumID, "AlbumID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("AlbumID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.AlbumID, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Contents, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Contents, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Contents, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.ReviewerEmail, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ReviewerEmail, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ReviewerEmail, "", 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>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

最佳答案

您的 View 需要将 Album.Title 传递回 POST 操作以验证它。目前(根据​​您发布的代码),Album 属性为空,因为它从未分配过实际的专辑,然后在您发布到操作的 View 代码中,您没有包含标题。

无论如何,您得到的异常是因为 Album prop 在您的代码中为 null,而您正在尝试验证其标题。

你还可以做的是更改验证码,只在专辑不为空时验证

When(x => x.Album != null, () => 
{
RuleFor(x => x.Contents).NotNull().When(x => x.Album.Title == "Disintegration");
});

关于c# - 外键的 FluentValidation RuleFor 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35607826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com