c# - jQuery中的ActionLink

标签 c# jquery asp.net-mvc actionlink

这是我的方法:

$(document).ready(function () {
    $('td.clickableCell').click(function () {
        var currentObject = null; 
        currentObject = $(this).text();
        @Html.ActionLink("GetThis", "Get", new {theName = currentObject} )
    });
});


但是它说currentObject在当前上下文中不存在。如何解决呢?

最佳答案

代替@Html.ActionLink,您应该使用jQuery.get函数。 @ Html.ActionLink在服务器上运行,而javascript在客户端上运行。

$(document).ready(function () {
    $('td.clickableCell').click(function () {
        var currentObject = $(this).text();
        $.get('@Url.Action("GetThis", "Get")', {theName : currentObject});
    });
});


Url.Action在服务器上呈现,并将为您提供适当的URL。 $.get将在客户端上运行get请求。

请记住,如果此javascript位于.js文件中,则将不会运行Url.Action。在这种情况下,您可能只想将其替换为/Get/GetThis或在页面上的隐藏字段中呈现url,并在.js文件中获取隐藏字段的值。

您需要一个如下所示的操作方法才能访问参数:

public ActionResult GetThis(string theName)
{
    // manipulate theName
    return View();
}

关于c# - jQuery中的ActionLink,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8824075/

相关文章:

c# - 多线程 TCP 服务器回显给所有客户端

c# - .PDF 文件的 IIS 7.5 404 错误

javascript - Bootstrap - 多个工具提示绑定(bind)到同一元素 ("html")

php - 缓存的 Ajax 调用

javascript - Jquery 将类添加到最后 5 个元素

c# - 我想在 C# 中将两个表单并排放置

C# MongoDB 驱动程序序列化 - 使用二进制文件

asp.net-mvc - ASP.Net MVC : What’s the Difference Between a Value Provider and Model Binder

asp.net-mvc - Sitecore 6.6、MVC 3 和 System.Web.Optimization?

ASP.NET Mvc 5 返回带有 seo 友好 url 的 View