Entity framework ASP.NET MVC 3仅使用自定义ValidationAttribute为集合中的前X项验证ComplexModel.EntityCollection.Property1
我的模型描述如下。我只需要Application.References集合中前两项的引用字段子集。我希望在客户端和服务器端验证中都会发生这种情况。我有一个工作方法,我将在下面描述,但我觉得客户端相当笨拙,所以我想知道是否有更好的方法来处理这个问题。如果可能的话,我希望是泛型的,因为我在应用程序对象中还有一些其他集合要验证 EF模型 我目前正在使用Steven Sanderson的RuleException和RulesViolationExceptionExtensions从手动执行服务器端验证,如下所示:Entity framework ASP.NET MVC 3仅使用自定义ValidationAttribute为集合中的前X项验证ComplexModel.EntityCollection.Property1,entity-framework,validation,asp.net-mvc-3,collections,validationattribute,Entity Framework,Validation,Asp.net Mvc 3,Collections,Validationattribute,我的模型描述如下。我只需要Application.References集合中前两项的引用字段子集。我希望在客户端和服务器端验证中都会发生这种情况。我有一个工作方法,我将在下面描述,但我觉得客户端相当笨拙,所以我想知道是否有更好的方法来处理这个问题。如果可能的话,我希望是泛型的,因为我在应用程序对象中还有一些其他集合要验证 EF模型 我目前正在使用Steven Sanderson的RuleException和RulesViolationExceptionExtensions从手动执行服务器端验证,
public static class ApplicationBusinessLogic
{
public static void RunServerValidation(Application app)
{
var errors = new RulesException<Application>();
for (int i = 0; i < 3; i ++)
{
if (string.IsNullOrEmpty(app.References.ElementAt(i).FullName))
errors.ErrorFor(x => x.References.ElementAt(i).FullName, "The Full Name for a first and second reference are required.");
if (app.References.ElementAt(i).ReferenceType == null)
errors.ErrorFor(x => x.References.ElementAt(i).ReferenceType, "The Reference Type for a first and second reference are required.");
if (app.References.ElementAt(i).RelationshipLength == null)
errors.ErrorFor(x => x.References.ElementAt(i).RelationshipLength, "The Relationship Length for a first and second reference are required.");
if (string.IsNullOrEmpty(app.References.ElementAt(i).Relationship))
errors.ErrorFor(x => x.References.ElementAt(i).Relationship, "The Relationship for a first and second reference are required.");
if (string.IsNullOrEmpty(app.References.ElementAt(i).Phone))
errors.ErrorFor(x => x.References.ElementAt(i).Relationship, "The Phone Number for a first and second reference are required.");
}
if (errors.Errors.Any())
throw errors;
}
}
当然,当我开始使用资源文件处理错误消息时,如果我决定将javascript移到.js文件中,可能会有点麻烦。我认为如果我将函数保持在视图中,我应该能够根据需要读取资源文件
[MetadataType(typeof(ReferenceMetadata))]
public partial class Reference
{
}
public class ReferenceMetadata
{
[DisplayFormat(NullDisplayText = "N/A")]
[DisplayName("Full Name")]
public string FullName { get; set; }
[DisplayFormat(NullDisplayText = "N/A")]
[DisplayName("Reference Type")]
public string ReferenceType { get; set; }
[DisplayFormat(NullDisplayText = "N/A")]
[DisplayName("How long have you known this person?")]
public string RelationshipLength { get; set; }
[DisplayFormat(NullDisplayText = "N/A")]
[DisplayName("Relationship")]
public string Relationship { get; set; }
[DisplayFormat(NullDisplayText = "N/A")]
[DisplayName("Phone")]
public string Phone { get; set; }
//...more properites
}
public static class ApplicationBusinessLogic
{
public static void RunServerValidation(Application app)
{
var errors = new RulesException<Application>();
for (int i = 0; i < 3; i ++)
{
if (string.IsNullOrEmpty(app.References.ElementAt(i).FullName))
errors.ErrorFor(x => x.References.ElementAt(i).FullName, "The Full Name for a first and second reference are required.");
if (app.References.ElementAt(i).ReferenceType == null)
errors.ErrorFor(x => x.References.ElementAt(i).ReferenceType, "The Reference Type for a first and second reference are required.");
if (app.References.ElementAt(i).RelationshipLength == null)
errors.ErrorFor(x => x.References.ElementAt(i).RelationshipLength, "The Relationship Length for a first and second reference are required.");
if (string.IsNullOrEmpty(app.References.ElementAt(i).Relationship))
errors.ErrorFor(x => x.References.ElementAt(i).Relationship, "The Relationship for a first and second reference are required.");
if (string.IsNullOrEmpty(app.References.ElementAt(i).Phone))
errors.ErrorFor(x => x.References.ElementAt(i).Relationship, "The Phone Number for a first and second reference are required.");
}
if (errors.Errors.Any())
throw errors;
}
}
@for (int i = 0; i < 3; i++)
{
ViewData.TemplateInfo.HtmlFieldPrefix = "Application.References[" + i + "]";
<h3>Reference @i</h3>
@Html.ValidationMessageFor(model => model.Application.References.ElementAt(i).FullName)
<div class="editor-label">
@Html.LabelFor(model => model.Application.References.ElementAt(i).FullName)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Application.References.ElementAt(i).FullName, new { maxlength = "100", data_val="true", data_val_required="The Full Name for a first and second reference are required." })
</div>
@* and so on for other properties... *@
}
@*End of Razor View file*@
<script type="text/javascript>
function addValidationRules() {
//references
for (var i = 0; i < 2; i++) {
$("#Application_References_" + i + "__FullName").rules("add", {
required: true,
messages: {
required: "The Full Name for a first and second reference are required."
}
});
//etc. for other properties
}//end for-loop
}//end addValidationRules()
$(document).ready(function () {
addValidationRules();
//more js code...
});
</script>