从类型lambda表达式中获取所有属性表达式(Get all property expressions from a type lambda expressions)
我想创建一个HtmlHelper,它可以为类的所有属性生成隐藏字段。
在我的剃刀页面中,我将调用以下内容:
@Html.HiddenForObject(x=>x.SomeClass)
其中x是使用@model SomeOtherClass定义的模型
我的助手被定义为=>
public static MvcHtmlString HiddenForObject<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
现在有些类有一些公共属性,我想为其创建隐藏字段。 但在这里,我被困住了,我需要找到正确的代码来迭代我的表达式中的所有属性,获取这些属性的表达式,以便我可以调用
//Pseudo code foreach(var propertyExpression in expression) { @Html.HiddenFor(expression); }
我尝试了一些东西,但基本上我没有想法。 任何帮助深表感谢。
I want to make a HtmlHelper that can produce hidden fields for all properties of a class.
In my razor page I will call the following:
@Html.HiddenForObject(x=>x.SomeClass)
where x would be the model defined with @model SomeOtherClass
My helper is defined as such =>
public static MvcHtmlString HiddenForObject<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
Now some class has a few public properties that I want to make hidden field for. But here I am stuck I need to find the right code to iterate over all the properties in my expression, get the expressions for those properties so I can call
//Pseudo code foreach(var propertyExpression in expression) { @Html.HiddenFor(expression); }
I tried a few things but basically I ran out of ideas. Any help is much appreciated.
原文:https://stackoverflow.com/questions/29850924
满意答案
您只需要在开始迭代之前应用逻辑来组织帖子的组织方式。
这是一个例子(这不是功能,只是一个快速说明):
<?php $col1 = array(); $col2 = array(); $posts = array('one', 'two', 'three', 'four', 'five', 'six'); foreach ($posts as $post) { $count++; $count % 2 === 1 ? array_push($col1, $post) : array_push($col2, $post); } ?> <div class="col-1"> <?php foreach ($col1 as $post) { echo '<div class="post">'.$post.'</div>'; } ?> </div> <div class="col-2"> <?php foreach ($col2 as $post) { echo '<div class="post">'.$post.'</div>'; } ?> </div>
这会产生输出:
<div class="col-1"> <div class="post">one</div> <div class="post">three</div> <div class="post">five</div> </div> <div class="col-2"> <div class="post">two</div> <div class="post">four</div> <div class="post">six</div> </div>
如您所见,现在$ posts数组的元素以交替的方式组织在两列中。
You just need to apply logic for how you want the posts organized before you start iterating them.
Here's an example (this isn't functional, just a quick illustration):
<?php $col1 = array(); $col2 = array(); $posts = array('one', 'two', 'three', 'four', 'five', 'six'); foreach ($posts as $post) { $count++; $count % 2 === 1 ? array_push($col1, $post) : array_push($col2, $post); } ?> <div class="col-1"> <?php foreach ($col1 as $post) { echo '<div class="post">'.$post.'</div>'; } ?> </div> <div class="col-2"> <?php foreach ($col2 as $post) { echo '<div class="post">'.$post.'</div>'; } ?> </div>
This produces the output:
<div class="col-1"> <div class="post">one</div> <div class="post">three</div> <div class="post">five</div> </div> <div class="col-2"> <div class="post">two</div> <div class="post">four</div> <div class="post">six</div> </div>
As you can see, now the elements of the $posts array are organized in both columns in an alternating fashion.
相关问答
更多重叠divs(Overlapping divs)
Float divs到两边(Float divs to both sides)
Javascript阻止重新循环通过div(Javascript prevent re-loop through divs)
如何重复循环div(How to repeatedly loop through divs)
while-loop divs没有反应(While-loop divs not reacting)
Wordpress - 如何在不重复DIV的情况下循环到两个不同的DIVS(Wordpress - How can I loop into two different DIVS without repeating the DIVs)
如何在不重复wordpress自定义帖子的情况下循环到两个不同的DIVS(How can I loop into two different DIVS without repeating in wordpress custom post)
appedChild()覆盖div(appedChild() overwriting divs)
jQuery一次淡入/淡出两个div,循环并重复(jQuery fade in/out two divs at a time, looping and repeating)
计算div中div的数量,在wordpress帖子的一页上有多个父div(Counting number of divs within a div, with multiple parent divs on one page of wordpress posts)
相关文章
更多java lambda表达式-JAVA8新特性
ServletOutputStream cannot be resolved to a type
怎样用Struts2的lambda表达式取一个map集合的子集
JAVA 正则表达式教程(超详细)三(续)
JAVA 正则表达式教程(超详细)二(续)
JAVA 正则表达式教程(超详细)一
【第五章】Spring表达式语言 之 5.4在Bean定义中使用EL—跟我学spring3
最新问答
更多您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
在计算机拍照在哪里进入
使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
单页应用程序:页面重新加载(Single Page Application: page reload)
在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!