Asp.net core 核心剃须刀页面-Id未发布在modal包装的表单上
我正试图从一个Asp.net core 核心剃须刀页面-Id未发布在modal包装的表单上,asp.net-core,razor-pages,Asp.net Core,Razor Pages,我正试图从一个标记提交一个模态内的表单。我可以用一个按钮来做,但我希望所有东西都统一,而不是把和标签混在一起 前端: @page @model AllCustomerModel @{ ViewData["Title"] = "AllCustomer"; } <h2>All Customers</h2> <p> <a asp-page="Customer">Create New Customer</a> </p&
标记提交一个模态内的表单。我可以用一个按钮来做,但我希望所有东西都统一,而不是把
和
标签混在一起
前端:
@page
@model AllCustomerModel
@{
ViewData["Title"] = "AllCustomer";
}
<h2>All Customers</h2>
<p>
<a asp-page="Customer">Create New Customer</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayName("Name")
</th>
<th>
@Html.DisplayName("Address")
</th>
<th>
@Html.DisplayName("Country")
</th>
<th>
@Html.DisplayName("City")
</th>
<th>
@Html.DisplayName("Phone")
</th>
<th>Edit | Delete</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.customerList)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
@Html.DisplayFor(modelItem => item.Country)
</td>
<td>
@Html.DisplayFor(modelItem => item.City)
</td>
<td>
@Html.DisplayFor(modelItem => item.Phone)
</td>
<td>
<a class="btn btn-primary" asp-page="./EditCustomer" asp-route-id="@item.CustomerID">Edit</a> |
@*<a class="btn btn-primary myButton" asp-page="./AllCustomer" onclick="return confirm('Are you sure you want to delete this item?');" asp-page-handler="Delete" asp-route-id="@item.CustomerID">Delete</a>*@
@* <a asp-page="./AllCustomer" OnClientClick="return ConfirmDelete(this)" asp-page-handler="Delete" asp-route-id="@item.CustomerID">Delete</a>*@
<a class="btn btn-primary myButton" data-toggle="modal" data-id="@item.CustomerID" data-target="#myModal">Delete</a>
@*<button type="button" class="btn btn-primary myButton" data-toggle="modal" data-id="@item.CustomerID" data-target="#myModal">
Delete
</button>*@
</td>
</tr>
}
</tbody>
</table>
<div class="modal fade" id="myModal">
<form id="myForm" method="post">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Delete Customer</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<input type="hidden" class="hiddenid" />
Are you sure you want to delete this customer?
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-danger">Yes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">No</button>
</div>
</div>
</div>
</form>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$('.myButton').on('click', function (e) {
var passedID = $(this).data('id');
console.log(passedID);
$(".modal-body .hiddenid").val(passedID);
});
</script>
@page
@模型AllCustomerModel
@{
ViewData[“标题”]=“所有客户”;
}
所有客户
创建新客户
@Html.DisplayName(“名称”)
@Html.DisplayName(“地址”)
@Html.DisplayName(“国家”)
@Html.DisplayName(“城市”)
@Html.DisplayName(“电话”)
编辑|删除
@foreach(Model.customerList中的var项)
{
@DisplayFor(modelItem=>item.Name)
@DisplayFor(modelItem=>item.Address)
@DisplayFor(modelItem=>item.Country)
@DisplayFor(modeleItem=>item.City)
@DisplayFor(modeleItem=>item.Phone)
编辑|
@*删除*@
@*删除*@
删除
@*
删除
*@
}
删除客户
&时代;
您确定要删除此客户吗?
对
不
$('.myButton')。在('click',函数(e)上{
var passedID=$(this.data('id');
控制台日志(passedID);
$(“.modal body.hiddenid”).val(passedID);
});
代码隐藏:
public class AllCustomerModel : PageModel
{
DatabaseContext _context;
public AllCustomerModel(DatabaseContext dbContext)
{
_context = dbContext;
}
public List<Customer> customerList { get; set; }
[BindProperty]
public Customer Customer { get; set; }
public void OnGet()
{
customerList = _context.CustomerTB.ToList();
}
public void OnPost(int? id)
{
Customer customer = Customer;
if (customer.CustomerID != null)
{
_context.Remove(_context.CustomerTB.Find(customer.CustomerID));
_context.SaveChanges();
}
customerList = _context.CustomerTB.ToList();
}
}
公共类AllCustomerModel:PageModel
{
数据库上下文(DatabaseContext)上下文;;
public AllCustomerModel(DatabaseContext dbContext)
{
_context=dbContext;
}
公共列表客户列表{get;set;}
[BindProperty]
公共客户客户{get;set;}
公共互联网
{
customerList=_context.CustomerTB.ToList();
}
邮政公开作废(int?id)
{
客户=客户;
if(customer.CustomerID!=null)
{
_Remove(_context.CustomerTB.Find(customer.CustomerID));
_SaveChanges();
}
customerList=_context.CustomerTB.ToList();
}
}
单击“是”按钮后,我可以在控制台中看到Id,因此在转换过程中似乎缺少了什么?我需要添加什么才能恢复Id帖子?post上未设置Id或客户Id
最后请注意,如果我使用下面的代码而不是
标记,那么一切都会按预期工作:
<button type="button" class="btn btn-primary myButton" data-toggle="modal" data-id="@item.CustomerID" data-target="#myModal">Delete</button>
删除
最简单的方法是使用Request.Form
集合访问基于字符串的索引。由于您在单击链接时已将值设置为输入区域:
$('.myButton').on('click', function (e) {
var passedID = $(this).data('id');
console.log(passedID);
$(".modal-body .hiddenid").val(passedID);
}
您可以将name
属性设置为隐藏输入:
<input type="hidden" class="hiddenid" name="ID" />
最简单的方法是使用
Request.Form
collection访问基于字符串的索引。由于您在单击链接时已将值设置为输入区域:
$('.myButton').on('click', function (e) {
var passedID = $(this).data('id');
console.log(passedID);
$(".modal-body .hiddenid").val(passedID);
}
您可以将name
属性设置为隐藏输入:
<input type="hidden" class="hiddenid" name="ID" />