在不重新加载页面的情况下在对话框上显示数据
Display Data on Dialogbox without reloading page
我正在尝试显示带有 ID + 名称 + 价格 的 bootstrap 对话框。
然后如果用户在对话框中选择 YES,则必须点击具有删除功能的 Action 方法并刷新页面上的数据以查看更改而无需重新加载页面。
我也不希望它点击删除用户操作方法后,它不能显示它的视图。
我尝试使用下面代码中的 ViewBag 但它没有显示 ID + 名称 + 价格 在 Bootstrap 对话框中,不重定向到删除操作方法,也不刷新页面
@model IEnumerable<School.Models.ApplicationUser>
<hr>
<table class="table table-responsive table-hover">
<tbody>
@foreach (var item in Model.OrderBy(x => x.DateTime))
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.ID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
<span style="color: #ff0000;">
<a class="btn btn-warning btn-sm disclaimer-dialog">
<i class="fa fa-unlock"> </i>Delete
ViewBag.MyId = @item.Id;
</a>
</span>
</td>
@ViewBag.MyId
</tr>
}
</tbody>
</table>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/Scripts/Views/SchoolAccounts/Delete.js")
}
<!-- Button trigger modal -->
<!-- Modal -->
<div class="modal fade" id="disclaimerModalDialog" tabindex="-1" role="dialog" aria-labelledby="exampleModalScrollableTitle" aria-hidden="true" data-keyboard="false" data-backdrop="static">
<div class="modal-dialog modal-dialog-scrollable" role="document">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title" id="exampleModalScrollableTitle">Confirmation Deletion</h3>
<button type="button" class="close" data-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p><strong>Are you sure you want to reset password for user ? @ViewBag.MyId </strong></p>
@using (Html.BeginForm("DeleteProduct", "SchoolAccounts",
FormMethod.Post, new
{
@id = "delete-form",
role = "form"
}))
{
@*@Html.HiddenFor(m => m.Id)
@Html.AntiForgeryToken()*@
}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default"
onclick="$('#delete-form').submit();">
Yes
</button>
<button type="button" class="btn btn-primary"
data-dismiss="modal">
No
</button>
</div>
</div>
</div>
</div>
Delete.js
的内容
$(function () {
$('.disclaimer-dialog').click(function () {
$('#disclaimerModalDialog').modal('show');
});
});
ViewBag 不能那样用。一旦 ViewBag 在页面上呈现,您就无法更新它的值。页面加载完成后,所有 razor 变量都是静态的。
我们需要做的是在 html 属性上分配这些值。
- 修改循环中的 link 以具有数据属性。我用了
data-id
, data-name
, data-price
;
@foreach (var item in Model.OrderBy(x => x.DateTime))
{
<tr>
@*just modify the link in the last column*@
<td>
<span style="color: #ff0000;">
<a data-id="@item.Id" data-name="@item.Name" data-price="@item.Price" class="btn btn-warning btn-sm disclaimer-dialog">
<i class="fa fa-unlock"> </i>
Delete
</a>
</span>
</td>
</tr>
}
- 修改您的 Delete.js 以访问这些属性并替换模态框的内容。
$(function () {
$('.disclaimer-dialog').click(function () {
// get attributes from the button
var id = $(this).data("id");
var name = $(this).data("name");
var price = $(this).data("price");
// Assign value to delete-id
$(".delete-id").val(id);
// update the first paragraph in modal-body
$('#disclaimerModalDialog').find(".modal-body p").eq(0).html("Are you sure you want to delete "+id+"-"+name+"-"+price+"?");
$('#disclaimerModalDialog').modal('show');
});
});
- 在您的模态正文中,将其用于输入字段。我们需要添加一个 class 以便我们可以轻松访问它;
@Html.HiddenFor(m => m.Id, new { @class="delete-id" });
- 将此功能添加到您的 Delete.js
$(function(){
$("#delete-form").submit(function(e){
// this will stop the page from refreshing or redirecting
e.PreventDefault();
var deleteId = $(".delete-id").val();
var passData = { id:deleteId };
// ajax call here
$.ajax({
type: "POST",
url: "/ControllerName/DeleteAjax",
data: JSON.stringify(passData),
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function(result){
alert(result);
// find the link with data-id == deleteid
// .parent() = span
// .parent().parent() = td
// .parent().parent().parent() = tr
// .remove() = remove that row
$("table a[data-id='"+deleteId+"']").parent().parent().parent().remove();
},
error: function(err){
alert(err);
}
});
});
});
- 在您的控制器中,添加此功能。
DeleteAjax
;
[HttpPost]
public ActionResult DeleteAjax(string id)
{
var product = context.Products.FirstOrDefault(p=>p.Id == id);
if(product == null){
return Content("error, cant find Id")
}else{
context.Products.Remove(product);
context.SaveChanges();
return Content("successfully deleted");
}
}
我正在尝试显示带有 ID + 名称 + 价格 的 bootstrap 对话框。
然后如果用户在对话框中选择 YES,则必须点击具有删除功能的 Action 方法并刷新页面上的数据以查看更改而无需重新加载页面。
我也不希望它点击删除用户操作方法后,它不能显示它的视图。
我尝试使用下面代码中的 ViewBag 但它没有显示 ID + 名称 + 价格 在 Bootstrap 对话框中,不重定向到删除操作方法,也不刷新页面
@model IEnumerable<School.Models.ApplicationUser>
<hr>
<table class="table table-responsive table-hover">
<tbody>
@foreach (var item in Model.OrderBy(x => x.DateTime))
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.ID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
<span style="color: #ff0000;">
<a class="btn btn-warning btn-sm disclaimer-dialog">
<i class="fa fa-unlock"> </i>Delete
ViewBag.MyId = @item.Id;
</a>
</span>
</td>
@ViewBag.MyId
</tr>
}
</tbody>
</table>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/Scripts/Views/SchoolAccounts/Delete.js")
}
<!-- Button trigger modal -->
<!-- Modal -->
<div class="modal fade" id="disclaimerModalDialog" tabindex="-1" role="dialog" aria-labelledby="exampleModalScrollableTitle" aria-hidden="true" data-keyboard="false" data-backdrop="static">
<div class="modal-dialog modal-dialog-scrollable" role="document">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title" id="exampleModalScrollableTitle">Confirmation Deletion</h3>
<button type="button" class="close" data-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p><strong>Are you sure you want to reset password for user ? @ViewBag.MyId </strong></p>
@using (Html.BeginForm("DeleteProduct", "SchoolAccounts",
FormMethod.Post, new
{
@id = "delete-form",
role = "form"
}))
{
@*@Html.HiddenFor(m => m.Id)
@Html.AntiForgeryToken()*@
}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default"
onclick="$('#delete-form').submit();">
Yes
</button>
<button type="button" class="btn btn-primary"
data-dismiss="modal">
No
</button>
</div>
</div>
</div>
</div>
Delete.js
的内容$(function () {
$('.disclaimer-dialog').click(function () {
$('#disclaimerModalDialog').modal('show');
});
});
ViewBag 不能那样用。一旦 ViewBag 在页面上呈现,您就无法更新它的值。页面加载完成后,所有 razor 变量都是静态的。
我们需要做的是在 html 属性上分配这些值。
- 修改循环中的 link 以具有数据属性。我用了
data-id
,data-name
,data-price
;
@foreach (var item in Model.OrderBy(x => x.DateTime))
{
<tr>
@*just modify the link in the last column*@
<td>
<span style="color: #ff0000;">
<a data-id="@item.Id" data-name="@item.Name" data-price="@item.Price" class="btn btn-warning btn-sm disclaimer-dialog">
<i class="fa fa-unlock"> </i>
Delete
</a>
</span>
</td>
</tr>
}
- 修改您的 Delete.js 以访问这些属性并替换模态框的内容。
$(function () {
$('.disclaimer-dialog').click(function () {
// get attributes from the button
var id = $(this).data("id");
var name = $(this).data("name");
var price = $(this).data("price");
// Assign value to delete-id
$(".delete-id").val(id);
// update the first paragraph in modal-body
$('#disclaimerModalDialog').find(".modal-body p").eq(0).html("Are you sure you want to delete "+id+"-"+name+"-"+price+"?");
$('#disclaimerModalDialog').modal('show');
});
});
- 在您的模态正文中,将其用于输入字段。我们需要添加一个 class 以便我们可以轻松访问它;
@Html.HiddenFor(m => m.Id, new { @class="delete-id" });
- 将此功能添加到您的 Delete.js
$(function(){
$("#delete-form").submit(function(e){
// this will stop the page from refreshing or redirecting
e.PreventDefault();
var deleteId = $(".delete-id").val();
var passData = { id:deleteId };
// ajax call here
$.ajax({
type: "POST",
url: "/ControllerName/DeleteAjax",
data: JSON.stringify(passData),
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function(result){
alert(result);
// find the link with data-id == deleteid
// .parent() = span
// .parent().parent() = td
// .parent().parent().parent() = tr
// .remove() = remove that row
$("table a[data-id='"+deleteId+"']").parent().parent().parent().remove();
},
error: function(err){
alert(err);
}
});
});
});
- 在您的控制器中,添加此功能。
DeleteAjax
;
[HttpPost]
public ActionResult DeleteAjax(string id)
{
var product = context.Products.FirstOrDefault(p=>p.Id == id);
if(product == null){
return Content("error, cant find Id")
}else{
context.Products.Remove(product);
context.SaveChanges();
return Content("successfully deleted");
}
}