c# - MVC 视图中的局部变量
问题描述
我有一个显示项目列表的 MVC 视图。使用视图自动生成的表在每一行都有这个操作链接:
@Html.ActionLink("Delete", "Delete", new { id = item.section_detail_id }).
这很好用,但我的要求是允许用户突出显示一行并单击列表底部的删除按钮。我需要一种方法让按钮知道当前突出显示了哪一行。所以我创建了这个局部变量:
@{int sectionDetailId = 0; }
然后我在表行上添加了一个 onclick 事件来存储被点击的行的 id:
tr id="@item.section_detail_id" onclick="addClass(this.id); sectionDetailId = @item.section_detail_id">
这是应该调出删除视图的按钮:
input type="button" value="Delete Employee" onclick="location.href='@Url.Action("Delete", "EmployeeList", new { id = sectionDetailId })'; alert(sectionDetailId)" />
无论单击哪一行,传递给视图的值始终为零。我在按钮上添加了警报功能,以显示变量具有正确的值。
有没有办法让 Action 方法使用变量或以其他方式从表中获取值?如果我对 ID 进行硬编码,则 Action 方法可以正常工作。
这是完整的视图:
@model IEnumerable<VCPDS2.Models.EmployeeListViewModel>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<style>
tr.highlight {
background-color: blue !important;
}
</style>
@{int sectionDetailId = 0; }
<h2>Index</h2>
@*<p>
@Html.ActionLink("Create New", "Create")
</p>*@
@if (Model != null)
{
<table id="employeeTable" class="table table-responsive table-striped">
<tr>
<th>
Last Name
</th>
<th>
First Name
</th>
<th>
Employee ID
</th>
<th>
Phone
</th>
<th>
Budget Unit
</th>
<th>
Division
</th>
</tr>
<tbody id="employeeList">
@foreach (var item in Model)
{
<tr id="@item.section_detail_id" onclick="addClass(this.id); sectionDetailId = @item.section_detail_id">
<td>
@Html.DisplayFor(modelItem => item.last_name)
</td>
<td>
@Html.DisplayFor(modelItem => item.first_name)
</td>
<td>
@Html.DisplayFor(modelItem => item.employee_id)
</td>
<td>
@item.phone_nbr
@* @Html.DisplayFor(modelItem => item.phone.area_code) @Html.DisplayFor(modelItem => item.phone.phone_nbr)*@
</td>
<td>
@item.BU
@*@Html.DisplayFor(modelItem => item.phone.BU)*@
</td>
<td>
@item.description
@*@Html.DisplayFor(modelItem => item.department.description)*@
</td>
<td>
@*@Html.ActionLink("Edit", "Edit", new { id = item.section_detail_id }) |
@Html.ActionLink("Details", "Details", new { id = item.section_detail_id }) |*@
@Html.ActionLink("Delete", "Delete", new { id = item.section_detail_id })
</td>
</tr>
}
</tbody>
</table>
<p>
@*@Html.ActionLink("Create New", "Create")*@
<input type="button" value="Add Employee"
onclick="location.href='@Url.Action("Create", "EmployeeList")'" />
<input type="button" value="Delete Employee"
onclick="location.href='@Url.Action("Delete", "EmployeeList", new { id =
@sectionDetailId })'; alert(sectionDetailId)" />
</p>
}
<script src="jquery-3.4.1.min.js"></script>
<script>
function addClass(row) {
//alert(row);
$('#employeeList').children().removeClass('highlight');
var element = document.getElementById(row);
element.classList.add("highlight");
}
</script>
@*@Html.ActionLink("Edit", "Edit", new { id = item.section_detail_id }) |
@Html.ActionLink("Details", "Details", new { id = item.section_detail_id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.section_detail_id
})*@
这是删除按钮的渲染 HTML:
<input type="button" value="Delete Employee" onclick="location.href='/EmployeeList/Delete/0'; alert(sectionDetailId)">
解决方案
推荐阅读
- amazon-web-services - API网关使用资源策略的问题
- mysql - 使用私有 IP 连接 Cloud Google 设置
- awk - 如何对 awk 捕获的浮点值求和
- json - 使用 Alamofire 和 SwiftyJson 在 Json 中获取服务器状态
- gpu - 如何在 EC2 CUDA GPU 实例中释放分配的 GiB 内存
- java - 如何使用 slf4j 以其他语言(如日语)记录文本
- azure - Rest API 以了解是否将外部原则 (IAM) 分配给 Azure 中的租户或订阅
- list - 如果 CAML 查询被修改或删除,SPList 不返回结果
- php - 向我的 xml 文件发送发布请求时无法接收响应
- java - 文本观察器,擦除 将文本向左移动