2016-02-24 75 views
2

當我運行程序我得到x.Album.Title == "Disintegration"一個System.NullReferenceException:FluentValidation RuleFor屬性

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

我怎麼可以設定此使Contents不被接受爲空時Album.Title == "Disintegration

型號

[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"); 
    } 
} 

控制器

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") 
} 
+0

你找到了解決辦法,只確認? –

回答

0

您的視圖需要將Album.Title傳回POST操作以驗證它。此刻(來自您發佈的代碼),相冊屬性爲空,因爲它從來沒有被分配過實際的相冊,那麼在視圖代碼中發佈到該動作的視圖代碼中,您沒有包含標題。

無論如何,你得到的例外是因爲你的代碼中的專輯prop爲null,而你試圖驗證它的標題。

你也可以做的是改變了驗證代碼,當專輯不爲空

​​