2

Assume

string value = ViewModelObject.SomeList[n].AnotherList[m].SomeProperty.ToString() //value is '5'

I need to turn this:

Expression<Func<ViewModelObjectType, object>> exp = x => x.SomeList[n].AnotherList[m].SomeProperty 

into

<input type='hidden' name='SomeList[n].AnotherList[m].SomeProperty' value='5'/>

where n and m are integers.

I'm betting someone's solved this before. I want to bind my javascript control to my page viewmodel in a type safe manner. I'm playing around with the expression classes now and I can extract the property as a string but the rest of it I haven't figured out yet.

Thanks!

5
  • Are you sure you want name to be "SomeList[n]..." instead of "SomeList[0]...", e.g.? Commented Jan 1, 2015 at 23:08
  • n and m are some integer like 0 Commented Jan 1, 2015 at 23:10
  • I know. But MVC generally binds well when you have property names that have constant integer values in the name property in HTML, instead of variable names. Commented Jan 1, 2015 at 23:13
  • I'm going to add the <input type=hidden name="foo.bar.Index" value='weirdindexvalue' /> so I can break free of the stiff index requirements, actually Commented Jan 1, 2015 at 23:21
  • The idea was to have my javascript control generate its own hidden inputs and keep them updated as the page changes. Sometimes they are deleted and in this case I wouldn't have to worry about regenerating inputs belonging to other controls of the same type. Commented Jan 1, 2015 at 23:26

2 Answers 2

4

The @Html.HiddenFor helper seems to do what you need:

@Html.HiddenFor(x => x.SomeList[n].AnotherList[m].SomeProperty)

But if for some reason you cannot rely on what's already built into the framework you could always roll your own stuff using the ExpressionHelper.GetExpressionText method which is what the ASP.NET MVC built-in helpers are already using:

public static class HtmlExtensions
{
    public static string Foo<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> ex)
    {
        return ExpressionHelper.GetExpressionText(ex);
    }
}

and then in your strongly typed view use it like that:

<input type='hidden' name='@Html.Foo(x => x.SomeList[n].AnotherList[m].SomeProperty)' value='5'/>
Sign up to request clarification or add additional context in comments.

3 Comments

Yes I was looking for a way around hidden for. I have some black magic to work.
Well, in this case you might also try the ExpressionHelper.GetExpressionText method as shown in my answer. But I'd rather try to fight the black magic at its root rather than extending it.
indeed, you've been a great help. I don't want to fiddle with the expression classes.
1

I had to use this courtesy of: ExpressionHelper.GetExpressionText(expression) not returning the name of my property

static public string GetExpressionText(LambdaExpression p)
{
    if (p.Body.NodeType == ExpressionType.Convert || p.Body.NodeType == ExpressionType.ConvertChecked)
    {
        p = Expression.Lambda(((UnaryExpression)p.Body).Operand,
            p.Parameters);
    }
    return ExpressionHelper.GetExpressionText(p);
}

My NodeType was always evaluating as ExpressionType.Convert

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.