本文整理汇总了C#中ModelMetadata.PropertySetter方法的典型用法代码示例。如果您正苦于以下问题:C# ModelMetadata.PropertySetter方法的具体用法?C# ModelMetadata.PropertySetter怎么用?C# ModelMetadata.PropertySetter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ModelMetadata
的用法示例。
在下文中一共展示了ModelMetadata.PropertySetter方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetProperty
/// <summary>
/// Updates a property in the current <see cref="ModelBindingContext.Model"/>.
/// </summary>
/// <param name="bindingContext">The <see cref="ModelBindingContext"/>.</param>
/// <param name="propertyMetadata">The <see cref="ModelMetadata"/> for the property to set.</param>
/// <param name="result">The <see cref="ModelBindingResult"/> for the property's new value.</param>
protected virtual void SetProperty(
ModelBindingContext bindingContext,
ModelMetadata propertyMetadata,
ModelBindingResult result)
{
if (bindingContext == null)
{
throw new ArgumentNullException(nameof(bindingContext));
}
if (propertyMetadata == null)
{
throw new ArgumentNullException(nameof(propertyMetadata));
}
if (!result.IsModelSet)
{
// If we don't have a value, don't set it on the model and trounce a pre-initialized value.
return;
}
if (propertyMetadata.IsReadOnly)
{
// The property should have already been set when we called BindPropertyAsync, so there's
// nothing to do here.
return;
}
var value = result.Model;
try
{
propertyMetadata.PropertySetter(bindingContext.Model, value);
}
catch (Exception exception)
{
AddModelError(exception, bindingContext, result);
}
}