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));
        }
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
imdadhusen
  • 2,493
  • 7
  • 40
  • 75

3 Answers3

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")
lancscoder
  • 8,658
  • 8
  • 48
  • 68
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.

Simon Bartlett
  • 2,010
  • 14
  • 15
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")
Yves Rochon
  • 1,492
  • 16
  • 28