c# - 来自 JsonResult 的动态 ActionLink?

标签 c# javascript jquery json asp.net-mvc-3

我正在开发 MVC3 应用程序。我认为我有一些逻辑想要转移到我的 Controller 。它根据模型的某些方面动态显示 ActionLink。 Html.ActionLink 的 linkText 和 actionName 参数是唯一不同的地方。我向 Controller 添加了一个方法,该方法将返回一个 JsonResult,其中包含 linkText 和 actionName 的字符串:

[AcceptVerbs(HttpVerbs.Get)]
public JsonResult GetActionButton(int id)
{
    string action = null;
    string text = null;

    // Snipped stuff that sets action and text

    return Json(new
    {
        buttonAction = action,
        buttonText = text
    });
}

我可以在哪里调用此方法来使用结果创建链接?

最佳答案

要从 Controller 生成链接,请查看 UrlHelper Methods ,最好使用 Action 来接收正常的 url。在使用 jquery 的客户端上,您可以创建如下链接:

$('<a>').attr('href', data.buttonAction).text(data.buttonText)

关于c# - 来自 JsonResult 的动态 ActionLink?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12571051/

相关文章:

c# - Windows 窗体中类似表格的简单视觉外观 (c#)

c# - 在 C# 中检索 RETURN @@IDENTITY 的值

javascript - d3有没有类似jQuery.closest(selector)的api?

javascript - 查看存储的 cookie 或在浏览器中调试

jquery - 与 jQuery 中的元素 this 在 Dojo 中等效吗?

c# - Azure 服务总线 "send"抛出 由于对象的当前状态,操作无效

c# - Xamarin iOS 共享按钮

javascript - Angular - 发出的事件无法到达父组件

jquery - 如何通过配置条目引用CSS中的背景图像url

javascript - 内容在 IE 中垂直展开,但在 Firefox 中有滚动条