This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ....other stuff here | |
paginate: 10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class SessionStateExtensions | |
{ | |
public static void Put<T>(this HttpSessionStateBase httpSession, T value) where T : class | |
{ | |
httpSession[typeof (T).FullName] = value; | |
} | |
public static void Put<T>(this HttpSessionStateBase httpSession, string key, T value) where T : class | |
{ | |
httpSession[typeof (T).FullName + key] = value; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class StringExtensions | |
{ | |
public static bool HasInvalidCharacters(this string value) | |
{ | |
//Ascii range between 32 and 127 | |
IEnumerable<int> lowRange = Enumerable.Range(0, 32); | |
IEnumerable<int> highRange = Enumerable.Range(128, 128); | |
IEnumerable<int> enumerable = value.ToCharArray().Select(x => Convert.ToInt32(((int)x).ToString())); | |
return lowRange.Intersect(enumerable).Count() != 0 || highRange.Intersect(enumerable).Count() != 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foreach( var validationResult in validationResults ) | |
{ | |
bindingContext.ModelState | |
.AddModelError( validationResult.MemberNames.FirstOrDefault(), validationResult.ErrorMessage ); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bindingContext.ModelState.AddModelErrors(validationResults); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class ModelStateExtensions | |
{ | |
public static void AddModelErrors(this ModelStateDictionary values, IEnumerable<ValidationResult> validationResults) | |
{ | |
foreach (var validationResult in validationResults) | |
values.AddModelError(validationResult.MemberNames.FirstOrDefault(), validationResult.ErrorMessage); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ValidationContext validationContext = new ValidationContext( someEditModel, null, null ); | |
List<ValidationResult> validationResults = new List<ValidationResult>(); | |
bool isValid = Validator.TryValidateObject( someEditModel, validationContext, validationResults ); | |
if( !isValid ) | |
{ | |
//do something maybe ... | |
bindingContext.ModelState.AddModelErrors(validationResults); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<ValidationResult> validationResults = someEditModel.ValidateAnnotations(); | |
if( validationResults.Count() > 0 ) | |
bindingContext.ModelState.AddModelErrors(validationResults); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class ValidationExtensions | |
{ | |
public static List<ValidationResult> ValidateAnnotations<T>(this T value) where T : class | |
{ | |
if(value == null) return new List<ValidationResult>(); | |
var validationContext = new ValidationContext(value, null, null); | |
var validationResults = new List<ValidationResult>(); | |
Validator.TryValidateObject(value, validationContext, validationResults, true); | |
return validationResults; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomerEditModel | |
{ | |
[Required] | |
[DisplayName("First Name")] | |
public string FirstName { get; set; } | |
[Required] | |
[DisplayName("Last Name")] | |
public string LastName { get; set; } | |
} |
OlderNewer