gpt4 book ai didi

asp.net-mvc - MVC Controller 方法在 HTML 沙发上返回 @Html.ActionLink 的语法是什么?

转载 作者:行者123 更新时间:2023-12-04 04:55:38 26 4
gpt4 key购买 nike

也许是重复的,但无法弄清楚如何在可调用的 MVC View .cs 文件中编写方法以在 cshtml 文件中返回等效的 @Html.ActionLink。

有点像:

public string BrowseMenu
{
return "<div><p>" + Html.ActionLink(" linktext ", "Action", "Controller") + "</p></div>";
}

我想要做的是将带有一些 ActionLink 的相同 HTML 结构返回到 MVC Controller 中的一堆不同的 View 页面,所以如果我以完全错误的方式处理它,这就是我想要到达的地方。

最佳答案

您需要返回 ActionResult从您的 Controller 查看,如果您想呈现一些 html。否则, View 将接收一个字符串并按原样显示它。

您可以定义一个包含 html 的局部 View ,并且可以在主 View 中呈现该局部 View 。

public PartialViewResult MyActionLink()
{
return PartialView("_MyPartialView");
}
_MyPartialView只是一个包含要呈现的 html 的 View 文件。

但是,我建议编写一个自定义的 html 帮助程序,我认为这是更清洁的解决方案。
public static string MyActionLink(this HtmlHelper helper)
{
StringBuilder sb = new StringBuilder();
sb.Append("<div><p>");
sb.Append(helper.ActionLink("Text","Action","Controller"));
sb.Append("</p></div>")
return sb.ToString();
}

然后你可以在你的 View 中呈现这个自定义的 html 助手
@Html.MyActionLink()

关于asp.net-mvc - MVC Controller 方法在 HTML 沙发上返回 @Html.ActionLink 的语法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16781109/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com