简体   繁体   English

多个Ajax.BeginForm无法正常工作C#MVC

[英]Multiple Ajax.BeginForm Not working c# mvc

I am deleting dynamically generated rows in html table using c# mvc.I am in success with first delete.Secondly i can only delete when i reload the page.That is only one delete is happening at one load what is my problem? 我正在使用c#mvc删除html表中动态生成的行。我第一次删除就成功了。其次,我只能在重新加载页面时删除。那是一次加载一次仅发生一次删除是什么问题?

Partial View _CartListing 部分视图_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>

Main View CartManager 主视图CartManager

<div class="mycontainer">   
  <div id="mydiv">
     @{Html.Action("CartManager","Cart");}   
  </div>   
</div> 

Controller 控制者

    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);
    }

Try 尝试

using (Ajax.BeginForm("DeleteCart", "Cart", new { cartid = ct.CartId }, new AjaxOptions()
{
    HttpMethod = "Post",
    OnSuccess = "onSuccess",
    UpdateTargetId = "mydiv",
    InsertionMode = InsertionMode.Replace,
}, new {id="Form"+i }))

Try changing the way you creating button id using Razor from delete_@i to delete_@(i) , other wise all buttons will have same id and it won't work as expected. 尝试将使用Razor创建按钮ID的方式从delete_@i更改为delete_@(i) ,否则所有按钮将具有相同的ID,并且将无法正常工作。

ie, change lines like this 即,像这样更改行

<input type="submit" value="delete" id="delete_@(i)"/>

I tested your .cshtml code like below and it works. 我测试了您的.cshtml代码,如下所示,它可以正常工作。 I think you need to double check the code in your controller where you are creating a new instance of db context (FoodContext in your case) in both Get and Post actions. 我认为您需要仔细检查在Get和Post动作中正在创建数据库上下文新实例(在您的情况下为FoodContext)的控制器中的代码。 Try using a single instance in the request context. 尝试在请求上下文中使用单个实例。

model 模型

 public class Article
    {
        public int Id { get; set; }
        public string ArticleContent { get; set; }
    }

_PartialView.cshtml _PartialView.cshtml

@model IEnumerable<WebApplication6.Models.Article>
    <table width="100%">

        @foreach (var ct in Model)
        {
            i = i + 1;
            using (Ajax.BeginForm("DeleteArticle", "Home", new { Id = ct.Id }, new AjaxOptions()
         {
             HttpMethod = "Post",
             OnSuccess = "onSuccess",
             UpdateTargetId = "mydiv"
         }, new { id = "Form" + i }))
            {
                <tr>
                    <td style="padding-left:20px;">
                        @ct.ArticleContent
                </td>
                <td style="padding-left:20px;">
                    @ct.Id
                </td>
                <td>
                    <input type="submit" value="delete" id="delete_@i" />
                </td>
            </tr>
            }
        }
    </table>

Controller.cs Controller.cs

 MyDbContext _db = new MyDbContext();
public ActionResult ArticleList()
        {
            var vm = _db.Articles.ToList();
            return PartialView("_CartListing", vm);
        }

        [HttpPost]
        public ActionResult DeleteArticle(int Id)
        {
            var a = _db.Articles.Find(Id);
            _db.Articles.Remove(a);
            _db.SaveChanges();

            var vm = _db.Articles.ToList();
            return PartialView("_CartListing", vm);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM