// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Linq.Expressions;
namespace Microsoft.AspNetCore.Mvc.Rendering
{
///
/// Value-related extensions for and .
///
public static class HtmlHelperValueExtensions
{
///
/// Returns the formatted value for the specified . Specifically, returns the
/// first non-null value found in:
/// the entry with full name,
/// the entry with full name, or
/// the evaluated against .
/// See for more information about a "full name".
///
/// The instance this method extends.
/// Expression name, relative to the current model.
/// A containing the formatted value.
///
/// Converts the expression result to a directly.
///
public static string Value(this IHtmlHelper htmlHelper, string expression)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
return htmlHelper.Value(expression, format: null);
}
///
/// Returns the formatted value for the specified . Specifically, returns the
/// first non-null value found in:
/// the entry with full name, or
/// the evaluated against .
/// See for more information about a "full name".
///
/// The instance this method extends.
/// An expression to be evaluated against the current model.
/// The type of the model.
/// The type of the result.
/// A containing the formatted value.
///
/// Converts the result to a directly.
///
public static string ValueFor(
this IHtmlHelper htmlHelper,
Expression> expression)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
if (expression == null)
{
throw new ArgumentNullException(nameof(expression));
}
return htmlHelper.ValueFor(expression, format: null);
}
///
/// Returns the formatted value for the current model. Specifically, returns the
/// first non-null value found in:
/// the entry with full name,
/// the entry with full name, or
/// the .
/// See for more information about a "full name".
///
/// The instance this method extends.
/// A containing the formatted value.
///
/// Converts the model value to a directly.
///
public static string ValueForModel(this IHtmlHelper htmlHelper)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
return htmlHelper.Value(expression: null, format: null);
}
///
/// Returns the formatted value for the current model. Specifically, returns the
/// first non-null value found in:
/// the entry with full name,
/// the entry with full name, or
/// the .
/// See for more information about a "full name".
///
/// The instance this method extends.
///
/// The format string (see https://msdn.microsoft.com/en-us/library/txafckwd.aspx) used to format the return
/// value unless that came from model binding.
///
/// A containing the formatted value.
///
/// Converts the model value to a directly if
/// is null or empty.
///
public static string ValueForModel(this IHtmlHelper htmlHelper, string format)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
return htmlHelper.Value(expression: null, format: format);
}
}
}