开发者

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

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


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


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜