qq_35693863
qq_35693863
2016-10-31 03:47
采纳率: 30.5%
浏览 1.3k
已采纳

.net MVC ajax提交表单问题

 <div id="hao">
<ul>
    <li id="ni">
        <span>全部好友</span>
        <span>@ViewBag.totalCount</span>
    </li>
    @foreach (MvcApplication1.Models.PrivateGroup m in ViewBag.Groups)
    {
        <li class="public">
            @Ajax.ActionLink(m.GroupName, 
            "ListFriendsByGroupId", "Friend", 
            new { groupId = m.GroupId },
            new AjaxOptions() {OnSuccess="loadFriendsByGroup" })
            <img src="@Url.Content("~/images/edit.gif")"/>
            <img src="@Url.Content("~/images/delete.gif")"/>
            <span>@m.FriendRelations.Count()</span>
        </li>
    }
    <li class="public">
        @Ajax.ActionLink("未分组好友", "ListFriendsByGroupId", "Friend",
        new { groupId = 1 }, new AjaxOptions() { OnSuccess="loadFriendsByGroup"})
        <span>@ViewBag.UnGroupCount</span>       
    </li>  
</ul>
@using (Ajax.BeginForm("Addgroup", "PrivateGroup",new AjaxOptions { HttpMethod = "Post",UpdateTargetId="hao", InsertionMode = InsertionMode.Replace }))
{ 
    @Html.TextBox("name")
    <input type="submit" value="确定"/>
    <input type="text" value="@ViewBag.id" name="id"/>
}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button>添加分组</button>
    </div>
    控制器代码
     public ActionResult Addgroup(string name,int? id)
    {
        db.PrivateGroups.Add(
            new PrivateGroup()
            {
                GroupName = name,
                OwnerId = (int)id
            });
        if (db.SaveChanges() < 0)
        {
            return View("Show");
        }
        return RedirectToAction("listMyGroups");
    }
    public ActionResult Show()
    {
        return View();
    }
    public ActionResult listMyGroups(int? id=1)
    {
        if(TempData["id"]!=null)
        id = (int)TempData["id"];
        //分组列表数据
        var groups = db.PrivateGroups.Where(a=>a.OwnerId==id).ToList();
        //未分组好友个数
        var unGroupCount = db.FriendRelations.Where(a=>a.UserId==id&&a.GroupId.HasValue==false).Count();
        //好友总数
        var totalCount = db.FriendRelations.Where(a=>a.UserId==id).Count();
        ViewBag.id = id;
        ViewBag.Groups = groups;
        ViewBag.UnGroupCount = unGroupCount;
        ViewBag.totalCount = totalCount;
        return PartialView("MyGroupsList");
    }
![图片说明](https://img-ask.csdn.net/upload/201610/31/1477885798_401405.png)
            那个Addgroup action 第一次点添加分组,它就执行了两遍,第二次就执行四遍,第三次执行八遍,成双倍的执行,为什么会这样?
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • qq_23663693
    -宛静- 2016-11-01 07:01
    已采纳

    是不是MyGroupsList 页面也调用的listMyGroups函数

    点赞 评论
  • caozhy

    return RedirectToAction("listMyGroups");
    ->
    return PartialView("listMyGroups");

    点赞 评论

相关推荐