如何呈现动作链接以查看表单控制器 [英] how to render actionlink to view form controller

查看:62
本文介绍了如何呈现动作链接以查看表单控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 ViewBag.htmml = "<li>" + "Html.ActionLink(" + "Home" + ", " + "Dashboard" + ", " + "User" + ")" + "</li>";

以上是我的代码,我正在尝试使用该代码将ActioLink呈现给我的视图.当代码运行时,使用以下代码,我没有获得名称为Home的操作链接,而是在<li>部分中得到了Html.ActionLink(Home, Dashboard, User).如何获得我所尝试的ActionLink.

Above is my code, using which I am trying render an ActioLink to my view. When the code runs, using the following code, I don't get an actionlink with name as Home, instead i get Html.ActionLink(Home, Dashboard, User) in my <li> part. How to get an ActionLink with what I am trying.

 @Html.Raw(ViewBag.htmml)

推荐答案

正如@Stephen Muecke所提到的,HTML需要在视图中生成,而不是在控制器中生成.

As @Stephen Muecke have mentioned, HTML needs to be generated in the View, not in the Controller.

但是,如果您坚持要在Controller中创建它,则需要有一个HtmlHelper可供您调用ActionLink方法.但是,顾名思义,HtmlHelper仅在视图中自动为您提供.

However, if you insist on creating it in the Controller, you need to have an HtmlHelper available for you to call the ActionLink method. But, as the name suggests, HtmlHelper is only available automatically for you in the View.

可以手动创建自己(例如,使用此答案中的解决方案),但是这是非常低效且容易出错的.

You can create one yourself manually (using a solution from this answer for example), but this is very inefficient and error-prone.

但是,由于您只是尝试生成一个<a>元素,并且已经在自己创建这些元素(如<li>元素),因此可以简单地执行此操作(使用UrlHelper >在控制器中也可用):

But, since you're only trying to generate an <a> element, and you already are creating the elements yourself (like the <li> element), you can simply do this (using the UrlHelper that is available in the Controller as well):

ViewBag.htmml = "<li><a href=\"" +
                 Url.Action("Dashboard", "User") +
                 "\">Home</a></li>";

这篇关于如何呈现动作链接以查看表单控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆