MVC3。阿贾克斯确认两次来 [英] MVC3. Ajax Confirmation comes twice
问题描述
我有产品的列表。每一行都有删除操作。当我尝试删除任何行一切都是真实的,但第二个删除后阿贾克斯确认来两次。请帮助。
有产品列表视图。
@model IEnumerable的< Domain.Entities.Product>
@ {
ViewBag.Title =产品;
布局=〜/查看/共享/ _AdminLayout.cshtml
}
< H1>产品< / H1>
<表类=网格>
&所述; TR>
<第i个姓名和LT; /第i
<第i说明< /第i
<第i价格与LT; /第i
< / TR>
@foreach(以型号VAR项){
&所述; TR>
< TD> @ item.Name< / TD>
< TD> @ item.Description< / TD>
< TD> @ item.Price< / TD>
&所述; TD>
@using(Ajax.BeginForm(DeleteProduct,管理,
新AjaxOptions {
确认=产品已删除!
UpdateTargetId =DeleteProduct
}))
{
@ Html.Hidden(ID,item.Id)
<输入类型=形象SRC =../图像/图标/ DeleteIcon.jpg/>
}
< / TD>
< / TR>
}
< /表>
有AdminController
[授权]
公共类AdminController:控制器
{
私人IProductRepository productRepository; 公共AdminController(IProductRepository productRepository)
{
this.productRepository = productRepository;
} 公众的ViewResult产品()
{
返回查看(productRepository.Products);
} [HttpPost]
公众的ActionResult DeleteProduct(INT ID)
{
产品PROD = productRepository.Products.FirstOrDefault(P => p.Id == ID);
如果(PROD!= NULL)
{
productRepository.DeleteProduct(正式版);
TempData的[消息] =的String.Format({0}已被删除,prod.Name);
}
返回RedirectToAction(产品);
}
}
最后_AdminLayout.cshtml
< HTML和GT;
< HEAD>
<标题> @ ViewBag.Title< /标题>
<链接HREF =@ Url.Content(〜/内容/ Admin.css)的rel =stylesheet属性类型=文/ CSS/>
<脚本的src =@ Url.Content(〜/脚本/ jQuery的-1.4.4.min.js)TYPE =文/ JavaScript的>< / SCRIPT>
<脚本的src =@ Url.Content(〜/脚本/ jquery.unobtrusive-ajax.js)TYPE =文/ JavaScript的>< / SCRIPT>
< /头>
<身体GT;
< DIV ID =DeleteProduct>
@if(TempData的[消息]!= NULL){
< DIV CLASS =消息> @TempData [消息] LT; / DIV>
}
@RenderBody()
< / DIV>
< /身体GT;
< / HTML>
这里的问题是,你是调用使用AJAX的 DeleteProduct
行动,这一行动正在执行重定向该产品采取行动。除了产品的动作返回一个完整的HTML,而不是局部的。所以,你得到的 jquery.unobtrusive-ajax.js
注射两次到你的DOM。所以,你得到的第二个2确认在第三等删除,3。
所以,通过定义包含表记录的部分开始(〜/查看/管理/ _Products.cshtml
)
@model IEnumerable的< Domain.Entities.Product>< DIV> @ViewData [消息] LT; / DIV><表类=网格>
<&THEAD GT;
&所述; TR>
<第i个姓名和LT; /第i
<第i说明< /第i
<第i价格与LT; /第i
< / TR>
< / THEAD>
<&TBODY GT;
@foreach(以型号VAR项)
{
&所述; TR>
< TD> @ item.Name< / TD>
< TD> @ item.Description< / TD>
< TD> @ item.Price< / TD>
&所述; TD>
@using(Ajax.BeginForm(DeleteProduct,管理,
新AjaxOptions {
确认=你确定你要删除这个产品吗?
UpdateTargetId =产品
})
)
{
@ Html.Hidden(ID,item.Id)
<输入类型=形象SRC =@ Url.Content(〜/图片/图标/ DeleteIcon.jpg),ALT =删除/>
}
< / TD>
< / TR>
}
< / TBODY>
< /表>
再修改主视图,以便它使用此部分:
@model IEnumerable的<产品与GT;@ {
ViewBag.Title =产品;
布局=〜/查看/共享/ _AdminLayout.cshtml
}< H1>产品< / H1>
< DIV ID =产品>
@ Html.Partial(_产品)
< / DIV>
和最后修改 DeleteProduct
控制器操作,因此不再做任何重定向,但删除记录后而是返回部分:
[HttpPost]
公众的ActionResult DeleteProduct(INT ID)
{
产品PROD = productRepository.Products.FirstOrDefault(P => p.Id == ID);
如果(PROD!= NULL)
{
productRepository.DeleteProduct(正式版);
计算机[消息] =的String.Format({0}已被删除,prod.Name);
} 返回PartialView(_产品,productRepository.Products);
}
I have a list of Products. Each line has 'Delete' action. When I try to delete any row everything is true, but after second deleting ajax confirmation comes twice. Please help.
There are product list view.
@model IEnumerable<Domain.Entities.Product>
@{
ViewBag.Title = "Products";
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
<h1>Products</h1>
<table class="Grid">
<tr>
<th>Name</th>
<th>Description</th>
<th>Price</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>@item.Name</td>
<td>@item.Description</td>
<td>@item.Price</td>
<td>
@using (Ajax.BeginForm("DeleteProduct", "Admin",
new AjaxOptions {
Confirm="Product was deleted!",
UpdateTargetId="DeleteProduct"
}))
{
@Html.Hidden("Id", item.Id)
<input type="image" src="../Images/Icons/DeleteIcon.jpg" />
}
</td>
</tr>
}
</table>
There are AdminController
[Authorize]
public class AdminController : Controller
{
private IProductRepository productRepository;
public AdminController(IProductRepository productRepository)
{
this.productRepository= productRepository;
}
public ViewResult Products()
{
return View(productRepository.Products);
}
[HttpPost]
public ActionResult DeleteProduct(int id)
{
Product prod = productRepository.Products.FirstOrDefault(p => p.Id == id);
if (prod != null)
{
productRepository.DeleteProduct(prod);
TempData["message"] = string.Format("{0} was deleted", prod.Name);
}
return RedirectToAction("Products");
}
}
And finally _AdminLayout.cshtml
<html>
<head>
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Admin.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
</head>
<body>
<div id="DeleteProduct">
@if (TempData["message"] != null) {
<div class="Message">@TempData["message"]</div>
}
@RenderBody()
</div>
</body>
</html>
The problem here is that you are calling the DeleteProduct
action with AJAX and this action is performing a Redirect to the Products action. Except that the Products action is returning a full HTML instead of a partial. So you get the jquery.unobtrusive-ajax.js
injected twice into your DOM. So you get 2 confirmations on the second delete, 3 on the third and so on.
So start by defining a partial containing the table records (~/Views/Admin/_Products.cshtml
):
@model IEnumerable<Domain.Entities.Product>
<div>@ViewData["message"]</div>
<table class="Grid">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Price</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.Name</td>
<td>@item.Description</td>
<td>@item.Price</td>
<td>
@using (Ajax.BeginForm("DeleteProduct", "Admin",
new AjaxOptions {
Confirm = "Are you sure you wanna delete this product?",
UpdateTargetId = "products"
})
)
{
@Html.Hidden("Id", item.Id)
<input type="image" src="@Url.Content("~/Images/Icons/DeleteIcon.jpg")" alt="Delete" />
}
</td>
</tr>
}
</tbody>
</table>
and then modify your main view so that it uses this partial:
@model IEnumerable<Product>
@{
ViewBag.Title = "Products";
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
<h1>Products</h1>
<div id="products">
@Html.Partial("_Products")
</div>
and finally modify your DeleteProduct
controller action so that it no longer does any redirects but returns the partial instead after deleting the record:
[HttpPost]
public ActionResult DeleteProduct(int id)
{
Product prod = productRepository.Products.FirstOrDefault(p => p.Id == id);
if (prod != null)
{
productRepository.DeleteProduct(prod);
ViewData["message"] = string.Format("{0} was deleted", prod.Name);
}
return PartialView("_Products", productRepository.Products);
}
这篇关于MVC3。阿贾克斯确认两次来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!