You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.9 KiB
88 lines
2.9 KiB
using System;
|
|
using System.Linq.Expressions;
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
using Microsoft.AspNetCore.Mvc.ViewFeatures;
|
|
|
|
namespace Infrastructure.Extensions
|
|
{
|
|
public static class ViewDataDictionaryExtensions
|
|
{
|
|
public static void Add(this ViewDataDictionary viewData, object name, object value, bool condition = true)
|
|
{
|
|
if (viewData is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(viewData));
|
|
}
|
|
if (name is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(name));
|
|
}
|
|
if (value is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(value));
|
|
}
|
|
if (condition)
|
|
{
|
|
viewData[name.ToString()] = value;
|
|
}
|
|
}
|
|
|
|
public static object Get(this ViewDataDictionary viewData, object name)
|
|
{
|
|
if (viewData is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(viewData));
|
|
}
|
|
if (name is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(name));
|
|
}
|
|
return viewData[name.ToString()];
|
|
}
|
|
|
|
public static void SelectList<TResult>(this ViewDataDictionary viewData, Expression<Func<object, TResult>> expression, Func<SelectList> getSelectListFunc, bool value = true)
|
|
{
|
|
if (viewData is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(viewData));
|
|
}
|
|
if (expression is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(expression));
|
|
}
|
|
if (getSelectListFunc is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(getSelectListFunc));
|
|
}
|
|
if (value)
|
|
{
|
|
var name = (expression.Body as MemberExpression).Member.Name + "SelectList";
|
|
viewData[name] = getSelectListFunc();
|
|
}
|
|
}
|
|
|
|
public static void MultiSelectList<TResult>(this ViewDataDictionary viewData, Expression<Func<object, TResult>> expression, Func<MultiSelectList> getSelectListFunc, bool value = true)
|
|
{
|
|
if (viewData is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(viewData));
|
|
}
|
|
if (expression is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(expression));
|
|
}
|
|
if (getSelectListFunc is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(getSelectListFunc));
|
|
}
|
|
if (value)
|
|
{
|
|
var name = (expression.Body as MemberExpression).Member.Name + "SelectList";
|
|
if (!viewData.ContainsKey(name))
|
|
{
|
|
viewData[name] = getSelectListFunc();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |