2

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)}">

with:

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

I tried some things:

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

aso...

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>

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.

UncleBob
  • 1,233
  • 3
  • 15
  • 33

5 Answers5

4

string fName = "accord" + i; is interpreted as ordinary HTML when between HTML nodes. Move it up right after the @for or wrap it in @{ //.. }

@for (var i = 0; i < 10; i++) {

    var fName = "accord" + i;

    <h3>i = @i</h3>
    <div id="@{@fName}">
        //..
    </div>
}

or

@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">
        //..
    </div>
}
devqon
  • 13,818
  • 2
  • 30
  • 45
  • 3
    Or just `
    `.
    – Ant P Jan 07 '15 at 12:37
  • 1
    You don't need additional variable, you can do it inline:
    – Dawid Rutkowski Jan 07 '15 at 12:37
  • @devqon: No, that's not it. The variable is recognised, and if I set a breakpoint, I can see its value. I tried putting it in @{} as you suggested, but that results in an error, stating that the statement already is in a code block. If I move it to where you suggested I don't get an error, but the results are the same as before. – UncleBob Jan 07 '15 at 13:03
  • @Ant P: As stated above, I tried that. It doesn't work. EDIT: Just saw that I didn't mention this particular formatting, but yes, I tried it. I'll try it again just to make sure... – UncleBob Jan 07 '15 at 13:04
  • @dawidr: Intellisense complains about a "NewLine in constant" at the closing > when I paste that line... no idea what that's about exactly. – UncleBob Jan 07 '15 at 13:05
  • Stupid mistake:) This will work:
    – Dawid Rutkowski Jan 07 '15 at 14:35
1

I do have same problem, and I found a solutions, In my case, not only I want to populate the attributes in my single page, but also want to pass a definite value to my partial view so that I may able to re-use it.

Warning: My codes looks awful, i'm still finding my way out to improve it.

  @{ ViewData["suffix"] = "RF"; }
  <div @{ViewData["myId"] = "foo" + ViewData["suffix"];}
     id=@ViewData["myId"]></div>

Now, before I call a partial view, I update the ViewData["suffix"] in order to ensure that all my replicated partial view has a unique id. Calling the partialView it would be

  <div id="partialView1">@{Html.RenderPartial("_partialView", ViewData);}</div>

I'm leaving this code, just in case someone needed it. cheers!

dr.Crow
  • 1,493
  • 14
  • 17
1
<div id="@($"text{i}")"></div>
Evgeny Sobolev
  • 515
  • 4
  • 13
0

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.

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: 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 ;)

UncleBob
  • 1,233
  • 3
  • 15
  • 33
0

string strDynamicId= "fuFileUpload_" + @item.FeildID;

<input type="file" accept=".pdf" name=@strDynamicId id=@strDynamicId />

This worked for me you should try this

Mohit
  • 15
  • 9