多Ajax.BeginForm不工作的C#MVC [英] Multiple Ajax.BeginForm Not working c# mvc

查看:100
本文介绍了多Ajax.BeginForm不工作的C#MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我删除使用C#mvc.I HTML表格动态生成行正在与第一delete.Secondly成功,我只能删除当我重新加载page.That只有一个删除是发生在一个负载什么是我的问题?

管窥_CartListing

  @model CartModel
<表格的宽度=100%>
    &所述; TR BGCOLOR =#E4E4E4>
       百分位风格=填充左:20像素;>
        FoodIem
       < /第i
       百分位风格=填充左:20像素;>
         数量
        < /第i
        百分位风格=填充左:20像素;>
        价钱
        < /第i
       百分位>< /第i
     < / TR>
    @ {
        INT I = 0;
        字符串K表;
     }
     @foreach(在Model.CartListings CartListing克拉)
     {
         I = I + 1;
   使用(Ajax.BeginForm(DeleteCart,购物车,新的{cartid = ct.CartId},新AjaxOptions()
    {
     列举HTTPMethod =邮报,
     的onSuccess =的onSuccess
     UpdateTargetId =mydiv
   },{新的id =形+ I}))
{
&所述; TR>
  < TD风格=填充左:20像素;>
     @ ct.CartId
  < / TD>
  < TD风格=填充左:20像素; >
    @ ct.Amount
  < / TD>
  < TD风格=填充左:20像素;>
    @ ct.Price
  < / TD>
  &所述; TD>
   <输入类型=提交值=删除ID =DELETE_ @我/>
  < / TD>
  < / TR>
 }
}
  &所述; TR bgcolor=\"#E4E4E4\"><td></td><td></td><td></td><td></td></tr>
&LT; /表&gt;

主要查看CartManager

 &LT; D​​IV CLASS =myContainer中&GT;
  &LT; D​​IV ID =mydiv&GT;
     @ {Html.Action(CartManager,车);}
  &LT; / DIV&GT;
&LT; / DIV&GT;

控制器

 公众的ActionResult CartList()
    {
        字符串用户=jaddu;
        FoodContext DB =新FoodContext();
        清单&LT; CartListing&GT; FD =(从E在db.FoodItems
                                  加盟澳中db.Carts上e.itemid等于o.itemid
                                  其中,o.username ==用户
                                  选择新CartListing
                                  {
                                   ITEMID = e.itemid,
                                   CartId = o.cartid,
                                   ITEMNAME = e.itemname,
                                   金额= o.amount,
                                   价格=(浮点)(e.price * o.amount)
                               })了ToList()。
          CartModel VM =新CartModel {CartListings = FD};
          返回PartialView(_ CartListing,VM);
    }    [HttpPost]
    公众的ActionResult DeleteCart(INT cartid)
    {
        字符串用户=jaddu;
        FoodContext DB =新FoodContext();
        车车= db.Carts.Single(F =&GT; f.cartid == cartid);
        db.Carts.DeleteObject(车);
        db.SaveChanges();
        清单&LT; CartListing&GT; FD =(从E在db.FoodItems
                                加盟澳中db.Carts上e.itemid等于o.itemid
                                其中,o.username ==用户
                                选择新CartListing
                                {
                                    ITEMID = e.itemid,
                                    CartId = o.cartid,
                                    ITEMNAME = e.itemname,
                                    金额= o.amount,
                                    价格=(浮点)(e.price * o.amount)
                                })了ToList()。
        CartModel VM =新CartModel {CartListings = FD};
        返回PartialView(_ CartListing,VM);
    }


解决方案

我测试你的.cshtml code像下面和它的作品。我认为你需要仔细检查code在您的控制器要在其中创建数据库上下文(FoodContext你的情况)的GET和POST操作的新实例。尝试在请求上下文中使用单个实例。

模式

 公共类文章
    {
        公众诠释标识{搞定;组; }
        公共字符串ArticleContent {搞定;组; }
    }

_PartialView.cshtml

  @model IEnumerable的&LT; WebApplication6.Models.Article&GT;
    &LT;表格的宽度=100%&GT;        @foreach(以型号VAR克拉)
        {
            I = I + 1;
            使用(Ajax.BeginForm(DeleteArticle,家,新{ID = ct.Id},新AjaxOptions()
         {
             列举HTTPMethod =邮报,
             的onSuccess =的onSuccess
             UpdateTargetId =mydiv
         },{新的id =形+ I}))
            {
                &所述; TR&GT;
                    &LT; TD风格=填充左:20像素;&GT;
                        @ ct.ArticleContent
                &LT; / TD&GT;
                &LT; TD风格=填充左:20像素;&GT;
                    @ ct.Id
                &LT; / TD&GT;
                &所述; TD&GT;
                    &LT;输入类型=提交值=删除ID =DELETE_ @我/&GT;
                &LT; / TD&GT;
            &LT; / TR&GT;
            }
        }
    &LT; /表&gt;

Controller.cs

  MyDbContext _db =新MyDbContext();
公众的ActionResult ArticleList()
        {
            变种的vm = _db.Articles.ToList();
            返回PartialView(_ CartListing,VM);
        }        [HttpPost]
        公众的ActionResult DeleteArticle(INT标识)
        {
            VAR一个= _db.Articles.Find(ID);
            _db.Articles.Remove(一);
            _db.SaveChanges();            变种的vm = _db.Articles.ToList();
            返回PartialView(_ CartListing,VM);
        }

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?

Partial View _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

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

解决方案

I tested your .cshtml code like below and it works. 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. 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

@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

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

这篇关于多Ajax.BeginForm不工作的C#MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆