本文整理汇总了C#中ModelStateDictionary.First方法的典型用法代码示例。如果您正苦于以下问题:C# ModelStateDictionary.First方法的具体用法?C# ModelStateDictionary.First怎么用?C# ModelStateDictionary.First使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ModelStateDictionary
的用法示例。
在下文中一共展示了ModelStateDictionary.First方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BindRouteProperties
/// <summary>
/// Binds controller properties decorated with <see cref="FromRouteAttribute"/>
/// using the current route values.
/// </summary>
/// <param name="controller">The controller to bind.</param>
/// <remarks>You can call this method from <see cref="ControllerBase.Initialize"/>.</remarks>
public static void BindRouteProperties(this ControllerBase controller) {
if (controller == null) throw new ArgumentNullException("controller");
var controllerData = controllerDataCache
.GetOrAdd(controller.GetType(), (type) => new ControllerData(type));
if (controllerData.Properties.Length == 0) {
return;
}
var modelState = new ModelStateDictionary();
var bindingContext = new ModelBindingContext {
FallbackToEmptyPrefix = true,
ModelState = modelState
};
for (int i = 0; i < controllerData.Properties.Length; i++) {
var propertyData = controllerData.Properties[i];
propertyData.BindProperty(controller.ControllerContext, bindingContext);
if (!modelState.IsValid) {
break;
}
}
if (!modelState.IsValid) {
ModelError error = modelState.First(m => m.Value.Errors.Count > 0).Value.Errors.First();
int statusCode = 404;
string message = "Not Found";
if (error.Exception != null) {
throw new HttpException(statusCode, message, error.Exception);
}
throw new HttpException(statusCode, message);
}
}
示例2: GetTheFirstPropertyWithAnError
private static string GetTheFirstPropertyWithAnError(ModelStateDictionary modelState, string prefixValue)
{
return modelState.First(x => x.Value.Errors.Count() > 0 && x.Key.StartsWith(prefixValue)).Key;
}