改变表达
我有一个很简短的问题。 在mvc中有一个静态扩展方法
System.Web.Mvc.Html.InputExtensions.HiddenFor(this HtmlHelper<TModel>htmlhelper,Expression<Func<TModel,TProperty>> expression,object htmlAttributes)
我使用这个方法来创建基于HiddenField的DropDownList。
public static MvcHtmlString CreateDropDown<TModel, TProperty, TKey, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, IEnumerable<ObjectData<TKey, TValue>> items, object htmlAttributes)
{
var resultVar = System.Web.Mvc.Html.InputExtensions.HiddenFor(helper, expression, htmlAttributes.ToRouteValueDictionary(new { @class = "DropDownInputHidden" }));
//some other code...
return resultVar;
}
对于简单类型属性,创建这样的隐藏字段很容易。在视图中,我这样使用它:
@Html.CreateDropDown(t=>t.SelectedValue,(some items list),(some attributes)) // t.SelectedValue is property of type string
但现在我想基于实现 IList 接口的属性创建许多隐藏字段。该函数应该如下所示:
public static MvcHtmlString CreateDropDown<TModel, TProperty, TKey, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, IEnumerable<ObjectData<TKey, TValue>> items, object htmlAttributes)
{
StringBuilder resultVar =new StringBuilder();
for (int i = 0; i < items.Count(); i++)
{
Expression<Func<TModel,TProperty>> ExpressionThatWillPointTo_i_Element = ???;
//ExpressionThatWillPointTo should be "based" on expression that is "pointing" to List<string>;
resultVar.Append(System.Web.Mvc.Html.InputExtensions.HiddenFor(helper, ExpressionThatWillPointTo_i_Element, htmlAttributes.ToRouteValueDictionary(new { @class = "DropDownInputHidden" })));
}
//some other code...
return MvcHtmlString.Create(resultVar.ToString());
}
之后我应该能够像这样调用这个修改后的函数:
@Html.CreateDropDown(t=>t.SelectedManyValues,(some items list),(some attributes)) // t.SelectedManyValuesis property of type List<string>
所以我需要以某种方式修改表达式以从表达式中获取每个值。
有人有一些想法吗?
I have a very short question.
In mvc there is a static extension method
System.Web.Mvc.Html.InputExtensions.HiddenFor(this HtmlHelper<TModel>htmlhelper,Expression<Func<TModel,TProperty>> expression,object htmlAttributes)
I`m using this method to create DropDownList based on HiddenField.
public static MvcHtmlString CreateDropDown<TModel, TProperty, TKey, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, IEnumerable<ObjectData<TKey, TValue>> items, object htmlAttributes)
{
var resultVar = System.Web.Mvc.Html.InputExtensions.HiddenFor(helper, expression, htmlAttributes.ToRouteValueDictionary(new { @class = "DropDownInputHidden" }));
//some other code...
return resultVar;
}
And as for simple type property it is easy to create such HiddenFields. In view i use it like this:
@Html.CreateDropDown(t=>t.SelectedValue,(some items list),(some attributes)) // t.SelectedValue is property of type string
But now i want to create many hidden fields based on property that implements IList interface. The function should look like this:
public static MvcHtmlString CreateDropDown<TModel, TProperty, TKey, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, IEnumerable<ObjectData<TKey, TValue>> items, object htmlAttributes)
{
StringBuilder resultVar =new StringBuilder();
for (int i = 0; i < items.Count(); i++)
{
Expression<Func<TModel,TProperty>> ExpressionThatWillPointTo_i_Element = ???;
//ExpressionThatWillPointTo should be "based" on expression that is "pointing" to List<string>;
resultVar.Append(System.Web.Mvc.Html.InputExtensions.HiddenFor(helper, ExpressionThatWillPointTo_i_Element, htmlAttributes.ToRouteValueDictionary(new { @class = "DropDownInputHidden" })));
}
//some other code...
return MvcHtmlString.Create(resultVar.ToString());
}
and after that i should been able to call this modified funcion like this:
@Html.CreateDropDown(t=>t.SelectedManyValues,(some items list),(some attributes)) // t.SelectedManyValuesis property of type List<string>
So what i need is to modify somehow the expression to get each value from the expression.
Anyone have some ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是我在您的情况下要做的事情,因为您已经开发了 CreateDropDown 并且可能想要重用该代码。
在
Shared/DisplayTemplates
中创建一个名为String.cshtml
的分部视图该视图的内容应该类似于:
之后,在主视图中,您可以执行以下操作
:我知道这是否适合你!
This is what I would do in your situation, since you already have developed CreateDropDown and might want to reuse that code.
Create a partial view called
String.cshtml
inShared/DisplayTemplates
The contents of that view should be something like:
After that, in the main view, you can just do:
Let me know if that works for you!!!