我有一个动态调用局部视图的视图。
模型对象示例:
public class ParentModel
{
public int id {get; set;}
public List<ChildModel> childModel {get;set;}
}
public class ChildModel
{
[NameRequired("eligibility","coupon","activeId")]
public string name {get; set;}
public string eligibility{get; set;}
public string coupon {get; set;}
public int activeId {get; set;}
}
查看示例:
ParentView.cshtml
- 使用父模型
@usng(Html.BeginForm())
{
-- additionalFields
<div id="addNewRow">
</div>
<input type="button" value "Add Row">
}
ChildView.cshtml
- 使用childModel
@using(Html.BeginCollectionItem())
{
--additional fields
}
我在点击按钮时使用jquery动态地将childView添加为addNewRow div的部分视图。
- 附加代码:
public class NameRequiredAttribute : ValidationAttribute, IClientValidatable
{
public NameRequiredAttribute (string eligibility,string coupon,string activeId)
: base(DefaultErrorMessage)
{
this.eligibility = eligibility;
this.coupon = coupon;
activeId = activeId;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
// server side validation code
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule()
{
ValidationType = "namerequired",
ErrorMessage = DefaultErrorMessage,
};
rule.ValidationParameters.Add("eligibility",BuildDependentPropertyId(metadata, context as ViewContext,this.eligibility));
rule.ValidationParameters.Add("coupon", BuildDependentPropertyId(metadata, context as ViewContext,this.coupon));
rule.ValidationParameters.Add("activeid", BuildDependentPropertyId(metadata, context as ViewContext,this.activeId));
yield return rule;
}
private string BuildDependentPropertyId(ModelMetadata metadata, ViewContext viewContext,string propertyName)
{
// build the ID of the property
string depProp = viewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(propertyName);
// unfortunately this will have the name of the current field appended to the beginning,
// because the TemplateInfo's context has had this fieldname appended to it. Instead, we
// want to get the context as though it was one level higher (i.e. outside the current property,
// which is the containing object (our Person), and hence the same level as the dependent property.
var thisField = metadata.PropertyName + "_";
if (depProp.StartsWith(thisField))
// strip it off again
depProp = depProp.Substring(thisField.Length);
return depProp;
}
}
Jquery:
$.validator.addMethod(
'namerequired',
function (value, element, params) {
var eligibility = params.eligibility;
var coupon = params.coupon;
var eligibility = params.eligibility;
var isin = params.isin;
var endingBal = params.endingbal;
var copoun = params.copoun;
var maturityDate = params.maturitydate;
var dependentElement = $('#' + eligibility);
var type = $(dependentElement).attr("type"),
actualVal = $(dependentElement).val();
alert(actualVal); // This is returning as undefined
});
$.validator.unobtrusive.adapters.add(
'namequired', ['eligibility', 'coupon', 'eligibility', 'activeid'], function (options) {
var params = {
trancheName: options.params.eligibility,
coupon: options.params.coupon,
activeid: options.params.activeid,
};
options.rules['namerequired'] = params;
options.messages['namerequired'] = options.message;
});
我的问题如下:
我需要在客户端将自定义数据注释添加到childModel&#34; name&#34;使用以下条件的财产:
仅当在任何一个或所有资格,优惠券,activeId UI字段中输入数据时,才为该特定名称行启用必填字段(这应该重复所有动态添加的行)
此致