【发布时间】:2014-03-02 05:59:48
【问题描述】:
我正在使用 c# mvc 在 html 表中删除动态生成的行。我第一次删除成功。其次,我只能在重新加载页面时删除。那是一次加载时只发生一次删除我的问题是什么?
部分视图_CartListing
@model CartModel
<table width="100%">
<tr bgcolor="#E4E4E4">
<th style="padding-left:20px;">
FoodIem
</th>
<th style="padding-left:20px;">
Quantity
</th>
<th style="padding-left:20px;">
Price
</th>
<th></th>
</tr>
@{
int i = 0;
string k;
}
@foreach (CartListing ct in Model.CartListings)
{
i = i + 1;
using (Ajax.BeginForm("DeleteCart", "Cart", new { cartid = ct.CartId }, new AjaxOptions()
{
HttpMethod = "Post",
OnSuccess = "onSuccess",
UpdateTargetId = "mydiv"
}, new {id="Form"+i }))
{
<tr>
<td style="padding-left:20px;">
@ct.CartId
</td>
<td style="padding-left:20px;" >
@ct.Amount
</td>
<td style="padding-left:20px;">
@ct.Price
</td>
<td>
<input type="submit" value="delete" id="delete_@i"/>
</td>
</tr>
}
}
<tr bgcolor="#E4E4E4"><td></td><td></td><td></td><td></td></tr>
</table>
主视图 CartManager
<div class="mycontainer">
<div id="mydiv">
@{Html.Action("CartManager","Cart");}
</div>
</div>
控制器
public ActionResult CartList()
{
string user = "jaddu";
FoodContext db = new FoodContext();
List<CartListing> fd = (from e in db.FoodItems
join o in db.Carts on e.itemid equals o.itemid
where o.username==user
select new CartListing
{
ItemId=e.itemid,
CartId=o.cartid,
Itemname =e.itemname,
Amount =o.amount,
Price=(float)(e.price*o.amount),
}).ToList();
CartModel vm = new CartModel { CartListings = fd };
return PartialView("_CartListing",vm);
}
[HttpPost]
public ActionResult DeleteCart(int cartid)
{
string user = "jaddu";
FoodContext db = new FoodContext();
Cart car = db.Carts.Single(f => f.cartid == cartid);
db.Carts.DeleteObject(car);
db.SaveChanges();
List<CartListing> fd = (from e in db.FoodItems
join o in db.Carts on e.itemid equals o.itemid
where o.username == user
select new CartListing
{
ItemId = e.itemid,
CartId = o.cartid,
Itemname = e.itemname,
Amount = o.amount,
Price = (float)(e.price * o.amount),
}).ToList();
CartModel vm = new CartModel { CartListings = fd };
return PartialView("_CartListing", vm);
}
【问题讨论】:
-
您是否收到错误消息?还是 mydiv 没有被替换?
-
只删除了一次。没有错误
标签: c# ajax asp.net-mvc-4