.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个回答

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

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐