改变表达

发布于 2024-12-04 03:54:59 字数 2352 浏览 1 评论 0原文

我有一个很简短的问题。 在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沙沙粒小 2024-12-11 03:54:59

这就是我在您的情况下要做的事情,因为您已经开发了 CreateDropDown 并且可能想要重用该代码。

Shared/DisplayTemplates 中创建一个名为 String.cshtml 的分部视图

该视图的内容应该类似于:

@model System.String

@Html.CreateDropDown(x => x, (some items list), (some attributes))

之后,在主视图中,您可以执行以下操作

@Html.DisplayFor(t => t.SelectedManyValues, (some items list), (some attributes))

:我知道这是否适合你!

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 in Shared/DisplayTemplates

The contents of that view should be something like:

@model System.String

@Html.CreateDropDown(x => x, (some items list), (some attributes))

After that, in the main view, you can just do:

@Html.DisplayFor(t => t.SelectedManyValues, (some items list), (some attributes))

Let me know if that works for you!!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文