假设
string value = ViewModelObject.SomeList[n].AnotherList[m].SomeProperty.ToString() //value is '5'
我需要转动这个:
Expression<Func<ViewModelObjectType, object>> exp = x => x.SomeList[n].AnotherList[m].SomeProperty
进入
<input type='hidden' name='SomeList[n].AnotherList[m].SomeProperty' value='5'/>
其中 n 和 m 是整数。
我敢打赌以前有人已经解决了这个问题。我想以类型安全的方式将我的 javascript 控件绑定(bind)到我的页面 View 模型。我现在正在研究表达式类,我可以将属性提取为字符串,但其余部分我还没有弄清楚。
谢谢!
最佳答案
@Html.HiddenFor
帮助器似乎可以满足您的需要:
@Html.HiddenFor(x => x.SomeList[n].AnotherList[m].SomeProperty)
但是,如果由于某种原因您不能依赖框架中已经内置的内容,您始终可以使用 ExpressionHelper.GetExpressionText
来推出自己的东西ASP.NET MVC 内置帮助程序已在使用的方法:
public static class HtmlExtensions
{
public static string Foo<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> ex)
{
return ExpressionHelper.GetExpressionText(ex);
}
}
然后在你的强类型 View 中使用它:
<input type='hidden' name='@Html.Foo(x => x.SomeList[n].AnotherList[m].SomeProperty)' value='5'/>
关于c# - 如何将指定属性的 lambda 表达式转换为表示相同属性的 asp.net mvc 兼容 'name' 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27735511/