Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Oct 12, 2011 04:59 AM by sudheervm
Member
8 Points
9 Posts
Oct 06, 2011 10:23 AM|LINK
On a controller page i am using two ActionResult functions and the same variable is being called on two different ActionResult functions, i want to take the value of the variable using viewbag as below. pleae guide what is the exact syntax.
@Html.ActionLink(ViewBag.Lang, ViewBag.Lang, "Home")
All-Star
24675 Points
4250 Posts
Oct 06, 2011 01:24 PM|LINK
@Html.ActionLink(ViewBag.Lang.ToString(), ViewBag.Lang.ToString(), "Home") does not work?
400 Points
168 Posts
Oct 06, 2011 08:46 PM|LINK
ViewBag nor ViewData dont work as actionlink parameter.
another option you have is that create new property of Lang in you model,
and do like this:
@Html.ActionLink(Model.Lang.ToString(),Model.Lang.ToString(), "Home")
i tested this and it worked.
Oct 06, 2011 08:49 PM|LINK
nevermind. this should work :
@Html.ActionLink(ViewData["Lang"].ToString(),ViewData["Lang"].ToString(), "Home")
so use ViewData["Lang"] instead of ViewBag.Lang in your controller.
the reason that viewbag doesnt work while viewdata works is i think viewbag is dynamic type while viewdata is an object.
correct me if i am wrong.
691 Points
177 Posts
Oct 07, 2011 11:29 PM|LINK
You just need to cast it, nothing else so write it:
@Html.ActionLink((string)ViewBag.Lang, (string)ViewBag.Lang, "Home")
21332 Points
1818 Posts
Microsoft
Oct 11, 2011 01:24 AM|LINK
Hi balach,
You are using this overload method of ActionLink():
ActionLink Method (HtmlHelper, String, String, String)
So you should change the value of the ViewBag to string. If it is still not work, please contact with me. About ActionLink Method, please check here:
http://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions.actionlink.aspx
Hope this helpful
Regards
Young Yang
60 Points
11 Posts
Oct 12, 2011 04:59 AM|LINK
Try this @{ string lang = ViewBag.Lang; @Html.ActionLink(lang, lang, "Home"); }
balach321
Member
8 Points
9 Posts
How to call viewbag value in cshtml in Html.ActionLink().
Oct 06, 2011 10:23 AM|LINK
On a controller page i am using two ActionResult functions and the same variable is being called on two different ActionResult functions, i want to take the value of the variable using viewbag as below. pleae guide what is the exact syntax.
raduenuca
All-Star
24675 Points
4250 Posts
Re: How to call viewbag value in cshtml in Html.ActionLink().
Oct 06, 2011 01:24 PM|LINK
@Html.ActionLink(ViewBag.Lang.ToString(), ViewBag.Lang.ToString(), "Home") does not work?
Radu Enuca | Blog
vampire203
Member
400 Points
168 Posts
Re: How to call viewbag value in cshtml in Html.ActionLink().
Oct 06, 2011 08:46 PM|LINK
ViewBag nor ViewData dont work as actionlink parameter.
another option you have is that create new property of Lang in you model,
and do like this:
@Html.ActionLink(Model.Lang.ToString(),Model.Lang.ToString(), "Home")
i tested this and it worked.
vampire203
Member
400 Points
168 Posts
Re: How to call viewbag value in cshtml in Html.ActionLink().
Oct 06, 2011 08:49 PM|LINK
nevermind. this should work :
@Html.ActionLink(ViewData["Lang"].ToString(),ViewData["Lang"].ToString(), "Home")
so use ViewData["Lang"] instead of ViewBag.Lang in your controller.
the reason that viewbag doesnt work while viewdata works is i think viewbag is dynamic type while viewdata is an object.
correct me if i am wrong.
theeyeabove
Member
691 Points
177 Posts
Re: How to call viewbag value in cshtml in Html.ActionLink().
Oct 07, 2011 11:29 PM|LINK
You just need to cast it, nothing else so write it:
Young Yang -...
All-Star
21332 Points
1818 Posts
Microsoft
Re: How to call viewbag value in cshtml in Html.ActionLink().
Oct 11, 2011 01:24 AM|LINK
Hi balach,
You are using this overload method of ActionLink():
So you should change the value of the ViewBag to string. If it is still not work, please contact with me. About ActionLink Method, please check here:
http://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions.actionlink.aspx
Hope this helpful
Regards
Young Yang
Feedback to us
Develop and promote your apps in Windows Store
sudheervm
Member
60 Points
11 Posts
Re: How to call viewbag value in cshtml in Html.ActionLink().
Oct 12, 2011 04:59 AM|LINK
Try this @{ string lang = ViewBag.Lang; @Html.ActionLink(lang, lang, "Home"); }