Creating HiddenFor IEnumerable in View

2019-06-16 13:16发布

问题:

I have a Property that is an IEnumerable

public IEnumerable<string> ChangesOthersResult { get; set; }

I need to collect all values from ChangesOthersResult and post from a view back to the controller. How can I loop through the Ienumerable and create hidden fields that will bind back to the ViewModel in the controller?

foreach(var item in Model.ChangesOthersResult)
   {
       @Html.HiddenFor(x => x.ChangesOthersResult);
   }

Is giving me the Raw SQL statement as text.

回答1:

Convert ChangesOthersResult to an array and use a for loop to output something like this:

for (int i = 0; i < Model.ChangesOthersResult.Length; i++ )    
{
   @Html.Hidden("ChangesOthersResult[" + i + "]", Model.ChangesOthersResult[i])
}


回答2:

I made this into an extension method so the for loop doesn't uglify your view code:

public static class HiddenExtensions
{
    public static MvcHtmlString HiddenForEnumerable<TModel, TProperty>(this HtmlHelper<TModel> helper,
        Expression<Func<TModel, IEnumerable<TProperty>>> expression)

    {
        var sb = new StringBuilder();

        var membername = expression.GetMemberName();
        var model = helper.ViewData.Model;
        var list = expression.Compile()(model);

        for (var i = 0; i < list.Count(); i++)
        {
            sb.Append(helper.Hidden(string.Format("{0}[{1}]", membername, i), list.ElementAt(i)));
        }

        return new MvcHtmlString(sb.ToString());
    }
}

GetMemberName is another extension method:

    public static string GetMemberName<TModel, T>(this Expression<Func<TModel, T>> input)
    {
        if (input == null)
            return null;

        if (input.Body.NodeType != ExpressionType.MemberAccess)
            return null;

        var memberExp = input.Body as MemberExpression;
        return memberExp != null ? memberExp.Member.Name : null;
    }

Hope this is helpful.



回答3:

Extended @GitteTitter 's solution for lists of custom objects:

@Html.HiddenForEnumerable(x => x.ValueTypes)    
@Html.HiddenForEnumerable(x => x.ViewModels, h=>h.Id, h=>h.Name)
@Html.HiddenForEnumerable(x => x.ViewModels, allPublicProps: true)

Source:

/// <summary>
/// Returns hiddens for every IEnumerable item, with it's selected properties, if any memberPropsExpression provided.
/// </summary>
public static MvcHtmlString HiddenForEnumerable<TModel, TModelProperty>(this HtmlHelper<TModel> helper,
    Expression<Func<TModel, IEnumerable<TModelProperty>>> expression, params Expression<Func<TModelProperty, object>>[] memberPropsExpressions)
{
    var sb = new StringBuilder();

    var membername = expression.GetMemberName();
    var model = helper.ViewData.Model;
    var list = expression.Compile()(model);

    var memPropsInfo = memberPropsExpressions.Select(x => new
    {
        MemberPropName = x.GetMemberName(),
        ListItemPropGetter = x.Compile()
    }).ToList();

    for (var i = 0; i < list.Count(); i++)
    {
        var listItem = list.ElementAt(i);
        if (memPropsInfo.Any())
        {
            foreach (var q in memPropsInfo)
            {
                sb.Append(helper.Hidden(string.Format("{0}[{1}].{2}", membername, i, q.MemberPropName), q.ListItemPropGetter(listItem)));
            }
        }
        else
        {
            sb.Append(helper.Hidden(string.Format("{0}[{1}]", membername, i), listItem));
        }
    }

    return new MvcHtmlString(sb.ToString());
}

/// <summary>
/// Returns hiddens for every IEnumerable item, with it's all public writable properties, if allPublicProps set to true.
/// </summary>
public static MvcHtmlString HiddenForEnumerable<TModel, TModelProperty>(this HtmlHelper<TModel> helper,
   Expression<Func<TModel, IEnumerable<TModelProperty>>> expression, bool allPublicProps)
{
    if (!allPublicProps)
        return HiddenForEnumerable(helper, expression);

    var sb = new StringBuilder();

    var membername = expression.GetMemberName();
    var model = helper.ViewData.Model;
    var list = expression.Compile()(model);

    var type = typeof(TModelProperty);
    var memPropsInfo = type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
        .Where(x => x.GetSetMethod(false) != null && x.GetGetMethod(false) != null)
        .Select(x => new
        {
            MemberPropName = x.Name,
            ListItemPropGetter = (Func<TModelProperty, object>)(p => x.GetValue(p, null))
        }).ToList();

    if (memPropsInfo.Count == 0)
        return HiddenForEnumerable(helper, expression);

    for (var i = 0; i < list.Count(); i++)
    {
        var listItem = list.ElementAt(i);
        foreach (var q in memPropsInfo)
        {
            sb.Append(helper.Hidden(string.Format("{0}[{1}].{2}", membername, i, q.MemberPropName), q.ListItemPropGetter(listItem)));
        }
    }

    return new MvcHtmlString(sb.ToString());
}

public static string GetMemberName<TModel, T>(this Expression<Func<TModel, T>> input)
{
    if (input == null)
        return null;

    MemberExpression memberExp = null;

    if (input.Body.NodeType == ExpressionType.MemberAccess)
        memberExp = input.Body as MemberExpression;
    else if (input.Body.NodeType == ExpressionType.Convert)
        memberExp = ((UnaryExpression)input.Body).Operand as MemberExpression;

    return memberExp != null ? memberExp.Member.Name : null;
}


回答4:

cannot use .ForEach(), since @Html.HiddenFor(x => x.ChangesOthersResult) will create the same element ID, which the model won't be recognize on postback.

for (int i = 0; i < Model.ChangesOthersResult.Count(); i++ )    
{
    @Html.HiddenFor(x => x.ChangesOthersResult[I]);
}


回答5:

How about a recursive approach?

public static MvcHtmlString HiddenForEnumerable<TModel, TModelProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, IEnumerable<TModelProperty>>> expression, string prefix = null)
{
    var sb = new StringBuilder();

    var membername = expression.GetMemberName();
    var model = htmlHelper.ViewData.Model;
    var list = expression.Compile()(model);

    var type = typeof(TModelProperty);
    var memPropertyInfo = type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
        .Where(x => x.GetSetMethod(false) != null && x.GetGetMethod(false) != null)
        .Select(x => new
        {
            Type = x.PropertyType,
            x.Name,
            Get = (Func<TModelProperty, object>)(p => x.GetValue(p, null))
        }).ToList();

    for (var i = 0; i < list.Count(); i++)
    {
        var listItem = list.ElementAt(i);
        if (memPropertyInfo.Any())
        {
            foreach (var m in memPropertyInfo)
            {
                var inputName = $"{prefix ?? ""}{membername}[{i}].{m.Name}";
                var inputValue = m.Get(listItem);
                var genericArguments = m.Type.GetGenericArguments();

                if (genericArguments.Any() && IsEnumerableType(m.Type))
                {
                    var delegateType = typeof(Func<,>).MakeGenericType(typeof(TModel), m.Type);
                    var bodyExpression = Expression.Constant(inputValue, m.Type);
                    var paramExpression = Expression.Parameter(typeof(TModel), "model");
                    var expressionTree = Expression.Lambda(delegateType, bodyExpression, new[] { paramExpression });
                    var hiddenForEnumerableInfo = typeof(HtmlHelpers).GetMethod("HiddenForEnumerable");
                    var hiddenForEnumerable = hiddenForEnumerableInfo.MakeGenericMethod(typeof(TModel), genericArguments[0]);
                    object[] args = { htmlHelper, expressionTree, inputName };

                    sb.Append(hiddenForEnumerable.Invoke(null, args));
                }
                else
                {
                    sb.Append(htmlHelper.Hidden(inputName, inputValue));
                }
            }
        }
        else
        {
            sb.Append(htmlHelper.Hidden($"{membername}[{i}]", listItem));
        }
    }

    return new MvcHtmlString(sb.ToString());
}

private static string GetMemberName<TModel, T>(this Expression<Func<TModel, T>> input)
{
    if (input == null)
        return null;

    MemberExpression memberExp = null;

    if (input.Body.NodeType == ExpressionType.MemberAccess)
        memberExp = input.Body as MemberExpression;
    else if (input.Body.NodeType == ExpressionType.Convert)
        memberExp = ((UnaryExpression)input.Body).Operand as MemberExpression;

    return memberExp?.Member.Name;
}

private static bool IsEnumerableType(Type type)
{
    return (type.GetInterface("IEnumerable") != null);
}

Example:

public class Model
{
    IEnumerable<Order> Orders { get; set; }
}

public class Order
{
    public int OrderId { get; set; }
    IEnumerable<Item> Items { get; set; }
}

public class Item
{
    public int ItemId { get; set; }
    public string Name { get; set; }
}

Usage:

@Html.HiddenForEnumerable(model => model.Orders)

Output:

<input id="Orders_0__OrderId" name="Orders[0].OrderId" type="hidden" value="1001">
<input id="Orders_0__Items_0__ItemId" name="Orders[0].Items[0].ItemId" type="hidden" value="201">
<input id="Orders_0__Items_0__Name" name="Orders[0].Items[0].Name" type="hidden" value="Something1">
<input id="Orders_0__Items_1__ItemId" name="Orders[0].Items[1].ItemId" type="hidden" value="202">
<input id="Orders_0__Items_1__Name" name="Orders[0].Items[1].Name" type="hidden" value="Something2">
<input id="Orders_1__OrderId" name="Orders[1].OrderId" type="hidden" value="1002">
<input id="Orders_1__Items_0__ItemId" name="Orders[1].Items[0].ItemId" type="hidden" value="205">
<input id="Orders_1__Items_0__Name" name="Orders[1].Items[0].Name" type="hidden" value="Something5">


回答6:

In your model's constructor, new up the IEnumerable ChagesOthersResult

public Model ()
{
  ChangesOthersResult = new List<string>();
}

Then in the view, use a for loop

for(int i = 0; i < Model.ChangesOthersResult.Count; i++)
{
  @Html.HiddenFor(x => x.ChangesOthersResult[i])
}


回答7:

the same for aspnetcore

using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;

namespace Proj.Helpers
{
    public static class HtmlHelpers
    {
        public static IHtmlContent HiddenForEnumerable<TModel, TModelProperty>(this IHtmlHelper<TModel> helper,
    Expression<Func<TModel, IEnumerable<TModelProperty>>> expression, params Expression<Func<TModelProperty, object>>[] memberPropsExpressions)
        {
            var hcb = new HtmlContentBuilder();

            var membername = expression.GetMemberName();
            var model = helper.ViewData.Model;
            var list = expression.Compile()(model);

            var memPropsInfo = memberPropsExpressions.Select(x => new
            {
                MemberPropName = x.GetMemberName(),
                ListItemPropGetter = x.Compile()
            }).ToList();

            for (var i = 0; i < list.Count(); i++)
            {
                var listItem = list.ElementAt(i);
                if (memPropsInfo.Any())
                {
                    foreach (var q in memPropsInfo)
                    {
                        hcb.AppendHtml(helper.Hidden(string.Format("{0}[{1}].{2}", membername, i, q.MemberPropName), q.ListItemPropGetter(listItem)));
                    }
                }
                else
                {
                    hcb.AppendHtml(helper.Hidden(string.Format("{0}[{1}]", membername, i), listItem));
                }
            }

            return hcb;
        }

        /// <summary>
        /// Returns hiddens for every IEnumerable item, with it's all public writable properties, if allPublicProps set to true.
        /// </summary>
        public static IHtmlContent HiddenForEnumerable<TModel, TModelProperty>(this IHtmlHelper<TModel> helper,
           Expression<Func<TModel, IEnumerable<TModelProperty>>> expression, bool allPublicProps)
        {
            if (!allPublicProps)
                return HiddenForEnumerable(helper, expression);

            var hcb = new HtmlContentBuilder();

            var membername = expression.GetMemberName();
            var model = helper.ViewData.Model;
            var list = expression.Compile()(model);

            var type = typeof(TModelProperty);
            var memPropsInfo = type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                .Where(x => x.GetSetMethod(false) != null && x.GetGetMethod(false) != null)
                .Select(x => new
                {
                    MemberPropName = x.Name,
                    ListItemPropGetter = (Func<TModelProperty, object>)(p => x.GetValue(p, null))
                }).ToList();

            if (memPropsInfo.Count == 0)
                return HiddenForEnumerable(helper, expression);

            for (var i = 0; i < list.Count(); i++)
            {
                var listItem = list.ElementAt(i);
                foreach (var q in memPropsInfo)
                {
                    hcb.AppendHtml(helper.Hidden(string.Format("{0}[{1}].{2}", membername, i, q.MemberPropName), q.ListItemPropGetter(listItem)));
                }
            }

            return hcb;
        }

        public static string GetMemberName<TModel, T>(this Expression<Func<TModel, T>> input)
        {
            if (input == null)
                return null;

            MemberExpression memberExp = null;

            if (input.Body.NodeType == ExpressionType.MemberAccess)
                memberExp = input.Body as MemberExpression;
            else if (input.Body.NodeType == ExpressionType.Convert)
                memberExp = ((UnaryExpression)input.Body).Operand as MemberExpression;

            return memberExp != null ? memberExp.Member.Name : null;
        }
    }
}