使用Razor在MVC中为div创建动态ID ... [英] creating dynamic id for divs in MVC with Razor... with a twist

查看:96
本文介绍了使用Razor在MVC中为div创建动态ID ...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

曲解了,我在该主题上找到的所有答案似乎都没有用.我几乎尝试了所有在此问题的答案中发现的格式,并尝试了几次绝望的尝试,但都没有任何效果.这几乎就像没有剃刀一样,但是在同一cshtml文件中的其他剃刀合成器执行起来却没有麻烦.在大多数情况下,div是在循环中声明的,并且其名称应附加i.这是我尝试过的一些事情,而不是按照我尝试的顺序进行.可以通过降低合成胶的健全程度来推断出该顺序:

The twist being, all answers that I found on the topic simply do not seem to work. I tried just about all formattings I found in answers to this question and made a few desperate attempts on my own, but none of them have any effect. It's almost as if razor wasn't there, but other razor synthax in the same cshtml file executes without trouble. As most of the time, the div is declared in a loop and its name should be appended by i. Here's some things I tried, not in the order I tried them in. The order can be deduced by the decreasing sanity of the synthax:

<div id="accord@(i)">

<div id="@("accord" + i)>

<div id="@{@("accord" + i)}">

具有:

string fName = "accord" + i;        //fName is filled correctly, that much I could verify

我尝试了一些事情:

<div id=@fName>

<div id="@fName">

<div id="@(fName)">

<div id="@{fName}">

<div id=@(fName)>

<div id="@{@fName}">

<div id=@{@fName}> 

我还尝试了两种方法的几种组合,例如

I also tried several combinations of the two approaches, like for example

string fName = "accord";
<div id="@(fName + i)">

还有...

所有这些尝试均未产生任何结果... div的ID始终为#Literaly_Whatever_Comes_after _ =

All of these attempts have not yielded any results... the id of the div is ALWAYS #Literaly_Whatever_Comes_after_=

所以...有人能冒险猜出我在做什么错吗?

So... can anybody venture a guess at what I'm doing wrong?

出于完成的目的,由于您永远不知道可能涉及的内容,因此这里是整个循环.它没有任何实际的逻辑,这是在我使概念生效之后出现的:

In the interest of completion, since you never know what might be involved, here's the entire loop. It doesn't have any actual logic in it, that comes after I made the concept work:

<div id="yearaccord">

    @for (int i = 0; i < 4; ++i)
    {
        //add a nested months accordion for every year
        <h3>i = @i</h3>
        string fName = "accord" + i;
        <div id="@{@fName}">

            @for (int j = 0; j < 4; ++j)
            {
                //add an accordion displaying the blog titles for each month
                <h3>j = @j</h3>
                <div>
                    @for (int w = 0; w < 6; ++w)
                    {
                        //add blog titles
                        <h4>JabbadaJba @w</h4>
                    }
                </div>
            }
        </div>
    }
</div>

请注意,此循环中的所有其他剃刀合成器都按照我预期的方式运行...仅当将其用作id时,事情才会发生变化.

Note that ALL other razor synthax in this loop behaves the way I expected it to... only when it's used as an id, things go wonky.

推荐答案

好,知道了...问题是我只是非常确信它没有用,而实际上却有用.结果看起来不像预期的那样,但这不是使我确信它没有起作用的原因.但是我总是在Visual Studio的调试器中检查div的ID.结果是Visual Studio不会显示div的实际名称,而是显示未声明的div的代码.最后,当我检查浏览器中的元素时,id是正确的.

Ok, got it figured out... The problem was that I was only very convinced that it didn't work, while it actually worked. The result didn't look as expected, but that's not what convinced me that it didn't work. But I always checked the id of the div in the debugger inside visual studio... turns out that visual studio does not show the actual name of the div, but the uninterpreted code with which it was declared. The id when I examined the element in the browser was correct, in the end.

仅是我对整个Web领域还很陌生,在此之前我只使用C ++(非.NET),所以我还不知道所有内容在哪里以及它们的行为方式……这是一部分我猜是学习过程的一部分.

Only that I'm pretty new to this whole web-thing, exclusively working with C++ (non .NET) before now, so I don't yet know where all the things are and how they behave... it's part of the learning process I guess.

感谢您的回答.一旦我知道在哪里工作,他们都会工作.

Thanks for the answers. They all worked once I knew where to look if they work.

@dawidr:您的行缺少右括号,因此产生了错误.仔细查看后,我注意到了它;)

@dawidr: Your line was missing a closing bracket, so that's what produced the error. I noticed it once I took a closer look at it ;)

这篇关于使用Razor在MVC中为div创建动态ID ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆