本文整理汇总了C#中ValidationContext.AddError方法的典型用法代码示例。如果您正苦于以下问题:C# ValidationContext.AddError方法的具体用法?C# ValidationContext.AddError怎么用?C# ValidationContext.AddError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ValidationContext
的用法示例。
在下文中一共展示了ValidationContext.AddError方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Validate
public void Validate(ValidationContext context)
{
if (string.IsNullOrWhiteSpace(this.EmailAddress) || this.EmailAddress.IndexOf('@') < 1)
{
context.AddError(nameof(this.EmailAddress), "Invalid email address.");
}
if (string.IsNullOrWhiteSpace(this.Password) || this.Password.Length < 8)
{
context.AddError(nameof(this.Password), "The password must be at least 8 characters long.");
}
}
示例2: Register
public async Task<ActionResult> Register(RegisterViewModel model)
{
var timeout = this.GetTimeoutToken();
var validationContext = new ValidationContext();
model.Validate(validationContext);
if (validationContext.HasErrors)
{
this.AddModelStateError(validationContext);
return await this.View(model, timeout);
}
var account = new Account
{
IdentityType = IdentityType.EmailAddress,
IdentityValue = model.EmailAddress.Trim().ToLowerInvariant(),
CreationTime = Clock.UtcNow,
Name = model.Name.Trim(),
PasswordHash = BCrypt.Net.BCrypt.HashPassword(model.Password),
};
try
{
await this.accountStore.Insert(account, this.GetTimeoutToken());
await this.CreateNewSession(account, false, timeout);
return this.RedirectToAction("Index", "Home");
}
catch (DuplicateKeyException)
{
validationContext.AddError(nameof(model.EmailAddress), "This email address is already in use.");
this.AddModelStateError(validationContext);
return await this.View(model, timeout);
}
}
示例3: AddMemberNameToHashSet
internal static bool AddMemberNameToHashSet(IEdmNamedElement item, HashSetInternal<string> memberNameList, ValidationContext context, EdmErrorCode errorCode, string errorString, bool suppressError)
{
IEdmSchemaElement schemaElement = item as IEdmSchemaElement;
string name = (schemaElement != null) ? schemaElement.FullName() : item.Name;
if (!memberNameList.Add(name))
{
if (!suppressError)
{
context.AddError(item.Location(), errorCode, errorString);
}
return false;
}
return true;
}
示例4: Login
public async Task<ActionResult> Login(LoginViewModel model)
{
var timeout = this.GetTimeoutToken();
var validationContext = new ValidationContext();
model.Validate(validationContext);
if (validationContext.HasErrors)
{
this.AddModelStateError(validationContext);
return await this.View(model, timeout);
}
var account = new Account
{
IdentityType = IdentityType.EmailAddress,
IdentityValue = model.EmailAddress.Trim().ToLowerInvariant(),
};
account = await this.accountStore.Get(account.PartitionKey, account.RowKey, timeout);
if (account == null || BCrypt.Net.BCrypt.Verify(model.Password, account.PasswordHash) == false)
{
validationContext.AddError("Cannot find a matching account and password. Please try again!");
this.AddModelStateError(validationContext);
return await this.View(model, timeout);
}
await this.CreateNewSession(account, model.RememberMe, timeout);
return this.RedirectToAction("Index", "Home");
}