1

In the ActionLink code below, how can I assign a C# string variable to @class in the view in ASP.NET MVC 5?

@Html.ActionLink("Manage List", "Index", new { @class = "DynamicClassName" });

I want to replace the static string @class = "DynamicClassName" with something dynamic, similar to @class = @myChangingColorClass

// Error 
// yes, myChangingColorClass is declared C# valid string 
@Html.ActionLink("Manage List", "Index", new { @class =  @myChangingColorClass });
3
  • Where do you declare myChangingColorClass exactly? Commented Jun 8, 2015 at 5:25
  • @ekad in the CSHTML view , there is no error, and viewing it in the debugger seems fine. Should I create the declaration code block right inside the new code block? Commented Jun 8, 2015 at 5:29
  • How and where is myChangingColorClass declared? What's the error message? Commented Jun 8, 2015 at 5:36

4 Answers 4

3

You need to declare myChangingColorClass in a code block in cshtml as below

@{
    string myChangingColorClass = "myClass";
}
Sign up to request clarification or add additional context in comments.

Comments

2

This can be do by two ways one is by setting value in ViewModel class or by setting value in ViewBag, ViewData or TempData.

Way 1) Preffered way Strongly Typed: Set css class name to viewmodel class attribute:

Class Student
{
  public ID BIGINT {get; set;}
  ... //other properties

}

Class StudentViewModel : Student
{
   public CssClass string {get; set;}
}

//controller action

public ActionResult Index(){
  StudentViewModel objModel; 
  //initialize model

  objModel.CssClass = "myCssClass"; //set css class name to viewmodel 
  return View(objModel);
}

//in view use code like below:

@model namespace.StudentViewModel;
@Html.ActionLink("Manage List", "Index", new { @class =  Model.CssClass })

Way 2) Set css class name to viewbag / viewdata / tempdate. But this is not prefered.

//controller action

public ActionResult Index(){

  ViewBag.CssClass = "myCssClass"; //set css class name to ViewBag
  //or
  ViewData["CssClass"] = "myCssClass"; //set css class name to ViewData
  //or
  TempData["CssClass"] = "myCssClass"; //set css class name to TempData

  return View();
}

//in view use code like below:

@Html.ActionLink("Manage List", "Index", new { @class =  @ViewBag.CssClass })
//Or
@Html.ActionLink("Manage List", "Index", new { @class =  @Convert.toString(ViewData["CssClass"]) })
//Or
@Html.ActionLink("Manage List", "Index", new { @class =  @Convert.toString(TempData["CssClass"]) })

Please let me know, is this works for you?

Comments

2

You can try putting a string variable (property) in your Model or ViewModel class and then just set it from code-behind and use it in your view like this :

@Html.ActionLink("Manage List", "Index", new { @class =  @Model.myChangingColorClass});

1 Comment

Answer should be in detail, it adds knowledge.
0
@{
    string myChangingColorClass = "myClass";
}

@Html.ActionLink("Manage List", "Index", null, new { @class = @myChangingColorClass });

it will generate

<a class="myClass" href="/ControllerName">Manage List</a>

but using this,

@Html.ActionLink("Manage List", "Index", new { @class = @myChangingColorClass });

it will generate

<a href="/ControllerName?class=myClass">Manage List</a>

it assumes it like a parameter of that method.. that u don't want..

add null before new { @class = @myChangingColorClass } will solve this problem

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.