我在mVC简单站点中使用ValidationAttribute进行了自定义验证概念,验证了日期范围。只对startDate感兴趣的是startDate。一切都很好。好吧,我没有客户端验证,但服务器端工作顺利。到目前为止一切顺利。
我现在的问题是,在我真正的解决方案中,我将表单作为局部视图。 scneario是一个简历表单,您可以在其中创建多个工作经验。我在父iew中包含_CreatejobExperience partialView并使用bpopup jquery插件显示它。
我实现了所有内容,就像我使用了充足的概念证据一样。但是当验证失败但弹出窗口未关闭时,错误消息不会显示。
我的验证课程是
public class DateRangeAttribute : ValidationAttribute
{
public string[] PropertyNames { get; set; }
public DateRangeAttribute(params string[] propertyNames)
{
this.PropertyNames = propertyNames;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var properties = this.PropertyNames.Select(validationContext.ObjectType.GetProperty);
var values = properties.Select(p => p.GetValue(validationContext.ObjectInstance, null)).OfType<DateTime>();
var startDate = values.FirstOrDefault();
var finishDate = (DateTime)value;
if (finishDate <= startDate)
{
return new ValidationResult("La fecha final debe ser posterior ala fecha de inicio");
}
return null;
}
}
属性和decorater设置如下
[Display(Name = "Entry date")]
[DataType(DataType.Date)]
[Required]
public DateTime EntryDate { get; set; }
[Display(Name = "Finish date")]
[DataType(DataType.Date)]
[Required]
[DateRange("EntryDate", ErrorMessage="Error message")]
public DateTime RetirementDate { get; set; }
在我的控制器中,我有像这样
在表单帖子上运行的操作 [HttpPost]
public PartialViewResult AddJobExperience(JobExperienceViewModel jobExperience)
{
if (ModelState.IsValid)
{
var resume = (ResumeViewModel)Session["tempResume"];
resume.JobExperienceViewModels.Add(jobExperience);
Session["tempResume"] = resume;
return PartialView("_ListJobExperience", resume.JobExperienceViewModels);
}
else
{
return PartialView("_CreateJobExperience");
}
}
在我的概念证明中,在我的控制器中我有类似的东西,
[HttpPost]
public PartialViewResult Index(ValidationModel model)
{
if(ModelState.IsVaid)
{
return RedirectToAction("Index", "Home");
}
else
{
return View();
}
}
我认为问题在于回归。如何使错误消息显示在局部视图中。
更新
父视图是控制器的索引视图,看起来有点像这样。我刚刚删除了不相关的部分
@using CAEWebSite.Models
@model CAEWebSite.Models.ResumeViewModel
@{
ViewBag.Title = "Hoja de Vida";
}
<div class="row">
@using (Ajax.BeginForm("Index", null, null, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>
@{var fullName = Model.FirstName +
(!String.IsNullOrWhiteSpace(Model.MiddleName) ? " " + Model.MiddleName : String.Empty) +
(!String.IsNullOrWhiteSpace(Model.LastName) ? " " + Model.LastName : String.Empty) +
(!String.IsNullOrWhiteSpace(Model.SecondLastName) ? " " + Model.SecondLastName : String.Empty);
}
@fullName
</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
<div class="col-md-12">
@Model.Email
</div>
</div>
<div class="form-group">
<div class="col-md-12">
@Model.UserName
</div>
</div>
<hr />
<h4>Job experiences</h4>
<div class="col-md-12">
<div id="divListJobExperience">
@Html.Partial("_ListJobExperience", Model.JobExperienceViewModels)
</div>
<p>
<a href="#" onclick="$('#newJobExperience').bPopup(); return false;">Nueva experiencia</a>
</p>
</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 id="newJobExperience" style="background-color:#fff; border-radius:5px;width:60%;">
<div class="col-md-12">
@Html.Partial("_CreateJobExperience", new JobExperienceViewModel())
</div>
</div>
<div>
@Html.ActionLink("Volver a la Lista", "Index", "SearchClient")
</div>
</div>