本文整理汇总了C#中ModelStateDictionary.TryGetValue方法的典型用法代码示例。如果您正苦于以下问题:C# ModelStateDictionary.TryGetValue方法的具体用法?C# ModelStateDictionary.TryGetValue怎么用?C# ModelStateDictionary.TryGetValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ModelStateDictionary
的用法示例。
在下文中一共展示了ModelStateDictionary.TryGetValue方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddModelStateErrors
public static void AddModelStateErrors(this RulesException exception, ModelStateDictionary modelState, string prefix, Func<ErrorInfo, bool> errorFilter)
{
if (errorFilter == null) throw new ArgumentNullException("errorFilter");
prefix = prefix == null ? "" : prefix + ".";
foreach (var errorInfo in exception.Errors.Where(errorFilter))
{
var key = prefix + errorInfo.PropertyName;
modelState.AddModelError(key, errorInfo.ErrorMessage);
ModelState existingModelStateValue;
if (modelState.TryGetValue(key, out existingModelStateValue) && existingModelStateValue.Value == null)
existingModelStateValue.Value = new ValueProviderResult(null, null, null);
}
}
示例2: AddModelStateErrors
public void AddModelStateErrors(ModelStateDictionary modelState, string prefix, Func<ErrorInfo, bool> errorFilter)
{
if (errorFilter == null) throw new ArgumentNullException("errorFilter");
prefix = prefix == null ? "" : prefix + ".";
foreach (var errorInfo in Errors.Where(errorFilter)) {
var key = prefix + errorInfo.PropertyName;
modelState.AddModelError(key, errorInfo.ErrorMessage);
// Workaround for http://xval.codeplex.com/WorkItem/View.aspx?WorkItemId=1297 (ASP.NET MVC bug)
// Ensure that some value object is registered in ModelState under this key
ModelState existingModelStateValue;
if(modelState.TryGetValue(key, out existingModelStateValue) && existingModelStateValue.Value == null)
existingModelStateValue.Value = new ValueProviderResult(null, null, null);
}
}
示例3: GridResponse
private GridResponse(ModelStateDictionary modelStateDictionary)
: this()
{
Success = modelStateDictionary.IsValid;
List<string> errors = new List<string>();
foreach (string key in modelStateDictionary.Keys)
{
ModelState modelState;
if (modelStateDictionary.TryGetValue(key, out modelState))
{
if (modelState.Errors.Any())
{
string error = string.Format("<b>{0}</b>: ", key);
error += string.Join(", ", modelState.Errors.Select(err => err.ErrorMessage));
errors.Add(error);
}
}
}
Message = string.Join("<br />", errors);
}
示例4: ShouldValidate
/// <summary>
/// Check if we should validate property.
/// If not, then clear out in ValueProvider.
/// </summary>
/// <param name="conditionPropertyName"></param>
/// <param name="validateIfNot"></param>
/// <param name="propertyName"></param>
/// <param name="containerName"></param>
/// <param name="instance"></param>
/// <param name="property"></param>
/// <param name="valueProvider"></param>
/// <param name="modelState"></param>
/// <returns>null if condition property is not in ValueProvider,
/// true if we should validate, false otherwise.</returns>
private static bool? ShouldValidate(string conditionPropertyName, bool validateIfNot, string propertyName, string containerName, object instance, PropertyInfo property, IValueProvider valueProvider, ModelStateDictionary modelState)
{
var valueProviderResult = valueProvider.GetValue(conditionPropertyName);
if (valueProviderResult == null)
{
// value for condition property is not supplied.
return null;
}
bool shouldValidate = (bool)valueProviderResult.ConvertTo(_boolType);
// invert ValidateIfNot will give us correct right condition for test.
bool condition = !validateIfNot;
if (shouldValidate == condition)
{
// let the normal validation handle it.
return true;
}
else
{
// set property on model equalt to default(T) where T is property type.
var v = property.PropertyType.IsValueType ? Activator.CreateInstance(property.PropertyType) : null;
property.SetValue(instance, v, null);
// also set ViewData.ModelState entry to default(T) where T is property type.
// or else Html.TextBoxFor() etc. will pick up value from request and render it.
ModelState ms;
string key = containerName != "" ? containerName + "." + propertyName : propertyName;
if (modelState.TryGetValue(key, out ms))
{
if (ms.Value != null)
{
// these properties has proteced setters, so need a bit of reflection to
// clear out.
var msValueType = ms.Value.GetType();
var propertyDesc = msValueType.GetProperty("RawValue");
propertyDesc.SetValue(ms.Value, v, null);
propertyDesc = msValueType.GetProperty("AttemptedValue");
propertyDesc.SetValue(ms.Value, null, null);
// remove validation errors. needed for WrapNumericModelValidator.
ms.Errors.Clear();
}
}
return false;
}
}