How to set Dynamic title of @Html.ActionLink from Controller?
Asked Answered
F

3

13

i have

@Html.ActionLink("Remove 1034, 1035, 1036", "RemoveSelected")

Now i want to set each id from Controller

for example:

@Html.ActionLink(ViewBag.RemoveSelectedTitle, "RemoveSelected") //this is not work

// GET: /TabMaster/
        public ActionResult Index()
        {
            ViewBag.RemoveSelectedTitle = "100,101,102";
            return View(_tabmasterService.GetTabMasterList(10, 1));
        }
Fiveandten answered 9/6, 2011 at 12:31 Comment(0)
D
35

You need to cast the RemoveSelectedTitle as a string. As your using the Viewbag this is a dynamic object and doesn't know the RemoveSelectedTitle is a string. You ActionLink should be something like:

@Html.ActionLink((string)ViewBag.RemoveSelectedTitle, "RemoveSelected")
Deese answered 9/6, 2011 at 12:55 Comment(0)
L
11

When using ViewBag in conjunction with HTML helpers that expect strings, you have to cast to a string, as follows:

@Html.ActionLink((string)ViewBag.RemoveSelectedTitle, "RemoveSelected")

This is because the compiler does not know the type of ViewBag.RemoveSelectedTitle due to ViewBag being a dynamic type.

Leyden answered 9/6, 2011 at 12:53 Comment(0)
D
1

I was trying to concatenate some hard coded text, and an int from the ViewBag, and found that this syntax worked:

@Html.ActionLink((string)("Remove Selected - # " + ViewBag.RemoveSelectedNumber.ToString()), "RemoveSelected")
Driscoll answered 17/9, 2014 at 18:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.