我有我的模型的強類型視圖窗體。問題是我似乎無法獲得正確的ID。我的觀點模型中它的形式模型,所以我要呼籲像一些要素:ASP.NET MVC2無法獲取元素的完整ID
<%: Html.TextBoxFor(m => m.Business.Business2)%>
問題是與此代碼:
viewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(partialFieldName)
如果我partialFieldName是「業務2」,它應該返回「Business_Business2」但它只返回「Business2」。但是,如果我使用
<%: Html.EditorFor(x => x.Business) %>
它返回「Business_Business2」。我無法使用Html.EditorFor,因爲我有css類放置在我的表單元素上。如果沒有正確的身份證,我不能做很多客戶端驗證,所以這實際上是在擾亂我。有任何想法嗎?
這裏是我的代碼:
模式
public class BusinessModel
{
public string Business1 { get; set; }
public string Business2 { get; set; }
}
public class AccountModel
{
public string Account1 { get; set; }
[MustMatch("Account1")]
public string Account2 { get; set; }
}
public class SampleWrappedModel
{
public BusinessModel Business { get; set; }
public AccountModel Account { get; set; }
}
驗證
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public sealed class MustMatchAttribute : ValidationAttribute
{
private const string DefaultErrorMessage = "Must match {0}";
public MustMatchAttribute(string propertyToMatch)
: base(DefaultErrorMessage)
{
PropertyToMatch = propertyToMatch;
}
public string PropertyToMatch { get; private set; }
public override string FormatErrorMessage(string name)
{
return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString, PropertyToMatch);
}
public override bool IsValid(object value)
{
throw new Exception("MustMatchAttribute requires the DataAnnotationsMustMatchValidator adapter to be registered");
}
}
//Adapter class
public class DataAnnotationsMustMatchValidator : DataAnnotationsModelValidator<MustMatchAttribute>
{
public DataAnnotationsMustMatchValidator(ModelMetadata metadata, ControllerContext context, MustMatchAttribute attribute)
: base(metadata, context, attribute)
{
}
public override System.Collections.Generic.IEnumerable<ModelValidationResult> Validate(object container)
{
var propertyToMatch = Metadata.ContainerType.GetProperty(Attribute.PropertyToMatch);
if (propertyToMatch != null)
{
var valueToMatch = propertyToMatch.GetValue(container, null);
var value = Metadata.Model;
bool valid = Equals(value, valueToMatch);
if (!valid)
{
yield return new ModelValidationResult { Message = ErrorMessage };
}
}
}
public override System.Collections.Generic.IEnumerable<ModelClientValidationRule> GetClientValidationRules()
{
string propertyIdToMatch = GetFullHtmlFieldId(Attribute.PropertyToMatch);
yield return new ModelClientMustMatchValidationRule(ErrorMessage, propertyIdToMatch);
}
private string GetFullHtmlFieldId(string partialFieldName)
{
ViewContext viewContext = (ViewContext)ControllerContext;
return viewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(partialFieldName);
}
}
public class ModelClientMustMatchValidationRule : ModelClientValidationRule
{
public ModelClientMustMatchValidationRule(string errorMessage, string propertyIdToMatch)
{
ErrorMessage = errorMessage;
ValidationType = "mustMatch";
ValidationParameters.Add("propertyIdToMatch", propertyIdToMatch);
}
}
@Timbo:是的。 – iamnobody