$.each(data, function (i, item) {
var stdid = item.Studentid;
var rows = "<tr>"
+ "<td class='prtoducttd'>" + item.Studentid + "</td>"
+ "<td class='prtoducttd'>" + item.StudentName + "</td>"
+ "<td class='prtoducttd'>" + item.StudentAddress + "</td>"
+ "<td class='prtoducttd'>"
+ '@Html.ActionLink("Edit", "Edit", new { id = item.Studentid })'
+ "</td>"
我在第一列中显示 Studentid,但在第四列中,当它与 html 帮助程序结合使用时(它说项目未在给定上下文中定义)item.Studentid 未显示在 @Html.ActionLink("Edit" , "Edit", new { id = item.Studentid }) 但它在上面的代码行中工作。您可以通过单击上面的链接来查看我的问题的图像。谢谢
是语法错误还是我必须使用其他一些 html 助手
ActionLink HTML Helper 方法在服务器上处理 Razor 页面时执行。 Razor 页面没有关于
item
变量是什么的任何上下文。 item
变量定义为客户端 (JavaScript) 变量,而不是服务器端 Razor 页面声明的变量。
您的代码的编写方式是,当在 AJAX
ActionLink
回调中执行 jQuery $.each(data, function (i, item)...)
迭代器时,它会尝试在客户端执行 success
方法。
您的成功回调正在构建一个 HTML 字符串,因此您需要在客户端上执行 JavaScript 时将锚元素添加为字符串:
+ "<td class='prtoducttd'>"
+ "<a href='/edit/" + item.Studentid + "'>Edit</a>"
+ "</td>"
或者使用字符串 literal 语法作为锚标记,这可能对代码可读性更好:
`<a href='/edit/${item.Studentid}'>Edit</a>`