Delmonte
Delmonte

Reputation: 411

Passing variables as parameters to @Url.Action

How to pass variables as parameters to @Url.Action ?

I populate a Menu control using @Url.Action.

List:
- childitem (it's populated from stored procedure)
Fields:
- TX_MENU: "My menu item"
- TX_ACTION: "Index"
- TX_CONTROLLER: "Buzon"

I'd like to populate my Menu control assigning

[email protected]("SomeAction", "SomeController")

How could I pass variables as parameters with @.

I tried this code, but failed:

<ul>
      @foreach (var itemchild in childitem)
      {
          <li class='last'><a href='@Url.Action(@itemchild.TX_ACTION, @itemchild.TX_CONTROLLER)'>@itemchild.TX_MENU</a>
          </li>
      }
</ul>

@itemchild.TX_ACTION and @itemchild.TX_CONTROLLER are not recognized as variables inside @Url.Action.

Upvotes: 0

Views: 345

Answers (1)

teo van kot
teo van kot

Reputation: 12491

You should white it like this:

<ul>
      @foreach (var itemchild in childitem)
      {
          <li class='last'><a href='@Url.Action(itemchild.TX_ACTION, itemchild.TX_CONTROLLER)'>@itemchild.TX_MENU</a>
          </li>
      }
</ul>

Don't escape your variables with @ symbol when you in Url.Action() helper

Upvotes: 1

Related Questions