当我在页面中编辑单个recored时,我使用复选框来获取一个选定的行而不是每一行都有一个actionlink元素,但似乎我不能通过调用javascript代码来实现这种方式(函数GetSelectedRow()应该返回一个id) 。谁能有个好主意呢?
<head runat="server">
<title>Index</title>
<script type="text/javascript" language="javascript">
function GetSelectedRow() {
var a = 0;
var chkBoxes = document.getElementsByName("chkSelect");
var count = chkBoxes.length;
for (var i = 0; i < count; i++) {
if (chkBoxes[i].checked == true)
a = chkBoxes[i].primaryKeyID;
}
return a;
}
</script>
</head>
<body>
<div>
<span style="width:20%">
<%: Html.ActionLink("Add", "Create")%>
</span>
<span>
<%: Html.ActionLink("Edit", "Edit", new { id = GetSelectedRow()) %>
</span>
<span>
<%: Html.ActionLink("Detial", "Details", new { id = GetSelectedRow() })%>
</span>
<span>
<%: Html.ActionLink("Delete", "Delete", new { id = GetSelectedRow()) %>
</span>
</div>
<table>
<tr>
<th></th>
<th>
CategoryID
</th>
<th>
CategoryName
</th>
<th>
Description
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%: Html.ActionLink("Details", "Details", new { id = item.AppCategoryID })%>
</td>
<td>
<%: Html.CheckBox("chkSelect", false, new { primaryKeyID = item.AppCategoryID })%>
</td>
<td>
<%: item.AppCategoryID %>
</td>
<td>
<%: item.AppCategoryName %>
</td>
<td>
<%: item.Description %>
</td>
</tr>
<% } %>
</table>
</body>
答案 0 :(得分:3)
你可以这样做:
<script type="text/javascript">
function RedirectUsingSelectedRow() {
var id = GetSelectedRow();
window.location = '../Controller/Details/' + id;
}
</script>
<a href="#" onclick = "RedirectUsingSelectedRow();">Edit</a>
答案 1 :(得分:0)
以这种方式混合服务器和客户端将无法正常工作。选择行时,您需要做的是操纵URL。因此,不要返回URL,而是让GetSelectedRow执行:
function GetSelectedRow() {
//existing logic minus return
var link1 = document.getElementById("Link1"); //this would require giving links an ID
link1.href = '<%= Url.Action("Detail", new { controller = "Details" }) %>' +
'id=' a.toString();
}
你必须从客户端更改它javascript是关键,而不是在渲染过程中这样做。
HTH。
答案 2 :(得分:0)
试试这个 -
$('#GetSelectedRow').click(function() { /* Your Code */ });
通过id&#39; GetSelectedRow&#39;调用java脚本函数。您可以直接调用函数
,而不是通过id调用函数<% Html.ActionLink("Edit", "Edit", "Controller", new { onclick = "GetSelectedRow();"}) %>