从类型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
2023-01-07 19:01

满意答案

您只需要开始迭代之前应用逻辑来组织帖子的组织方式。

这是一个例子(这不是功能,只是一个快速说明):

<?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)

我想你想要这样的东西: HTML <div class="parent"> <div class="a"></div> <div class="b"></div> </div> CSS .parent { position: relative; } .a { position: absolute; width: 100px; height: 100px; z-index: 100; background: red; } .b { position: abs...

Float divs到两边(Float divs to both sides)

请参阅: http : //jsfiddle.net/thirtydot/qdZWh/ HTML: <div class="left"></div> <div class="right"></div> <div class="left"></div> <div class="right"></div> CSS: div { width: 50px; height: 50px; outline: 1px dashed #666 } .left { float: le...

Javascript阻止重新循环通过div(Javascript prevent re-loop through divs)

在连接之前删除单击并触摸事件。 每次调用loopCalendar方法时,都会附加一个附加的事件处理程序。 On(document.getElementById(l),['click','touch'],function(e){})这是问题所在的行。 您需要通过删除附加的先前处理程序来反转此效果,或者您需要确保仅为每个元素调用一次。 Remove the click and touch event before you attach. An additional event handler gets...

如何重复循环div(How to repeatedly loop through divs)

请尝试以下。 var yogNumber = 0; var yogs = document.querySelectorAll('.yog'); function next() { yogNumber++; if (yogs.length > yogNumber) { document.querySelector('.active').classList.remove('active'); yogs[yogNumber].classList.add('acti...

while-loop divs没有反应(While-loop divs not reacting)

ID的定义必须是唯一的。 你可以使用类来代替。 while($row = mysql_fetch_array($result)){ $output = "<div class='drag'>$row[id] - $row[name] - $row['email']</div>"; echo $output; } 然后... $(".drag").effect("highlight", {}, 2000); IDs must be unique by definition. Yo...

Wordpress - 如何在不重复DIV的情况下循环到两个不同的DIVS(Wordpress - How can I loop into two different DIVS without repeating the DIVs)

您只需要在开始迭代之前应用逻辑来组织帖子的组织方式。 这是一个例子(这不是功能,只是一个快速说明): <?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_...

如何在不重复wordpress自定义帖子的情况下循环到两个不同的DIVS(How can I loop into two different DIVS without repeating in wordpress custom post)

问题有点模糊,但我假设你真正想要做的只是改变每个第二个div的类left-grid和right-grid 。 在这种情况下,您不必重复整个div,只需更改类。 这可以在“柜台”( $i )的帮助下完成。 <?php $featuresitems = new WP_Query(array( 'post_type' => 'scbleftfeatures' )); $i = 0; ?> <?php while( $featuresitems->have_posts()) : $featu...

appedChild()覆盖div(appedChild() overwriting divs)

你的else只创建一个新元素 - 请注意它在循环之前只调用一次document.createElement('div') ? 然后,您覆盖该div的内容并调用document.body.appendChild(divs) ,但.appendChild()不会创建其他副本,而是移动现有元素。 您需要在else的for循环内添加对document.createElement('div')另一个调用。 let beans = ["1", "2", "3", ["4", "5", "6"]]; //...

jQuery一次淡入/淡出两个div,循环并重复(jQuery fade in/out two divs at a time, looping and repeating)

你可以这样做: var quotes = $('#container .quote'); function cycleQuotes () { var current = quotes.filter(".active"), next; if (current.length == 0 || (next = current.next().next()).length == 0 ) { // first iteration or last, select first tw...

计算div中div的数量,在wordpress帖子的一页上有多个父div(Counting number of divs within a div, with multiple parent divs on one page of wordpress posts)

您需要使用.each()函数,然后选择每个父元素并仅对该单个元素的子元素进行数学.each() 。 试试这个 $('.colourbox').each(function(i) { // select visible children var visibleDivs = $(this).find('div').length; // use whatever width calculation you'd like... var targetWidth = 300 /...

相关文章

更多

java lambda表达式-JAVA8新特性

Lambda概述 lambda表示数学符号“λ”,计算机领域中λ代表“λ演算”,表达了计算机中最基本 ...

ServletOutputStream cannot be resolved to a type

在使用jsp生成web图片时遇到这个问题,这是源代码中的一条语句,源代码可以执行,可是一将源码放入ec ...

怎样用Struts2的lambda表达式取一个map集合的子集

前台的jsp代码是这样的。 &lt;s:select cssStyle=&quot; width:2 ...

JAVA 正则表达式教程(超详细)三(续)

匹配的模式(Pattern flags) compile( )方法还有一个版本,它需要一个控制正则表达 ...

JAVA 正则表达式教程(超详细)二(续)

第二点是,在本地号码位的前三位和后四位数字间有可能是空格符,而不是连字号,更有胜者,或根本就没有分隔符 ...

JAVA 正则表达式教程(超详细)一

在regex包中,包括了两个类,Pattern(模式类)和Matcher(匹配器类)。Pattern类 ...

【第五章】Spring表达式语言 之 5.4在Bean定义中使用EL—跟我学spring3

SpEL支持在Bean定义时注入,默认使用“#{SpEL表达式}”表示,其中“#root”根对象默认可 ...

最新问答

更多

您如何使用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?)

将diff文件复制到存储库的根目录,然后执行以下操作: git apply yourcoworkers.diff 有关apply命令的更多信息, apply 见其手册页 。 顺便说一下:一个更好的方法是通过文件交换整个提交文件是发送者上的命令git format-patch ,然后在接收器上加上git am ,因为它也传送作者信息和提交信息。 如果修补程序应用程序失败,并且生成diff的提交实际上在您的备份中,则可以使用尝试在更改中合并的apply程序的-3选项。 它还适用于Unix管道,如下

将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)

尝试将第二行更改为snprintf(buf1, sizeof buf1, "%.2f", balance1); 。 另外,为什么要声明用该特定表达式分配缓冲区的存储量? EDIT @LưuVĩnhPhúc在下面的评论中提到我的原始答案中的格式说明符将舍入而不是截断,因此根据如何在不使用C舍入的情况下截断小数,您可以执行以下操作: float balance = 200.56866; int tmp = balance1 * 100; float balance1 = tmp / 100.0; c

OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)

这是简单的解决方案 在你需要写的控制器中 BackendMenu::setContext('Archetypics.Team', 'website', 'team'); 请参阅https://octobercms.com/docs/backend/controllers-views-ajax#navigation-context BackendMenu::setContext('Author.Plugin name', 'Menu code', 'Sub menu code'); 你需要在r

页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)

每当发出请求时ASP都会创建一个新的Page对象,并且一旦它将响应发送回用户就不会保留对该Page对象的引用,因此只要你找不到某种方法来保持生命自己引用该Page对象后,一旦发送响应, Page和只能通过该页面访问的所有对象才有资格进行垃圾回收。 ASP creates a new Page object whenever a request is made, and it does not hold onto the reference to that Page object once it

codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)

要在生产服务器中调试这个,你可以临时放 error_reporting(E_ALL); 并查看有哪些其他错误阻止正确的重定向。 您还应该检查生产服务器发送的响应标头。 它是否具有“缓存”,是否需要重新验证标头等 to debug this in production server, you can temporary put error_reporting(E_ALL); and see what other errors are there that prevents the proper

在计算机拍照在哪里进入

打开娥的电脑.在下面找到视频设备点击进去就可以了...

使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)

你是对的。 第一次输入后,换行符将保留在输入缓冲区中。 第一次读取后尝试插入: cin.ignore(); // to ignore the newline character 或者更好的是: //discards all input in the standard input stream up to and including the first newline. cin.ignore(numeric_limits::max(), '\n'); 您必须为#inc

No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)

for (int k = 0; k > 10; k++) { System.out.println(k); } k不大于10,所以循环将永远不会执行。 我想要什么是k<10 ,不是吗? for (int k = 0; k < 10; k++) { System.out.println(k); } for (int k = 0; k > 10; k++) { System.out.println(k); } k is not greater than 10, so loop

单页应用程序:页面重新加载(Single Page Application: page reload)

优点是不注销会避免惹恼用户,以至于他们会想要杀死你:-)。 说真的,如果每次刷新页面时应用程序都会将我注销(或者在新选项卡中打开一个链接),我再也不会使用该应用程序了。 好吧,不要这样做。 确保身份验证令牌存储在刷新后的某个位置,即不在某些JS变量中,而是存储在cookie或本地存储中。 The advantage is that not logging off will avoid pissing off your users so much that they'll want to kill

在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)

EXECUTE IMMEDIATE 'SELECT '||field_val_temp ||' FROM tableb WHERE function_id = :func_val AND rec_key = :rec_key' INTO field_val USING 'STDCUSAC' , yu.rec_key; 和, EXECUTE IMMEDIATE 'UPDATE tablec SET field_val_'||i||' = :field_val' USI