为什么这会循环

时间:2015-03-07 09:12:16

标签: c# asp.net-mvc razor cycle

我遇到了有趣的问题。首先点击我的部分视图提交,一切正常。它进入db并保存我的记录。但如果我第二次点击它(当第一次点击将结束他的工作时),它会两次并保存所有内容两次。 ,然后点击上一个* 2 。任何人都可以解释发生了什么?

它的Facebook应用程序,但我不认为它的fb问题

我的控制器是:

KubetiDbContext db = new KubetiDbContext();

    public ActionResult Index()
    {
        string request = Session["signed_request"].ToString();
        FaceBook client = new FaceBook();
        FBuserinfo user = new FBuserinfo();
        SignedRequest sr = client.parseSignedRequest(request);

        var db_user = (from a in db.Users
                       where a.fbId == sr.user_id
                       select a).FirstOrDefault();

        ViewBag.ID = db_user.id;


        //var participants = from q in db.fifthWeekBids
        var laptop = (from a in db.Laptops
                      select a).FirstOrDefault();

        ViewBag.countLeft = laptop.countLeft;

        return View();
    }

    [HttpGet]
    public ActionResult Bet()
    {
        var laptop = (from a in db.Laptops
                      select a).FirstOrDefault();

        ViewBag.startPrice = laptop.startPrice;
        return YourPlace();
    }

    [HttpPost]
    public ActionResult Bet(string code)
    {
        string request = Session["signed_request"].ToString();
        FaceBook client = new FaceBook();
        FBuserinfo user = new FBuserinfo();
        SignedRequest sr = client.parseSignedRequest(request);

        var db_user = (from a in db.Users
                       where a.fbId == sr.user_id
                       select a).FirstOrDefault();

        using (KubetiDbContext _db = new KubetiDbContext())
        {
            firstWeekBid fw = new firstWeekBid();
            fw.userId = db_user.id;
            fw.bidCount = int.Parse(code);
            fw.dateOfBid = DateTime.Now;
            _db.firstWeekBids.Add(fw);
            _db.SaveChanges();
        }


        if (Request.IsAjaxRequest())
        {
            if (ViewData["IsAjax"] == null || (bool)ViewData["IsAjax"] != false)
            {
                ViewData["IsAjax"] = true;
            }
            return Bet();
        }

        return View();
    }

    public ActionResult YourPlace()
    {

        return View();
    }

视图:

<div class="bigBgPaper">
<div id="regLogo"></div>
<div id="regAucHead"></div>
<div id="rightRed">

</div>

<nav class="mainNav">
    @Html.ActionLink("ჩემი პროფილი", "Index", "Profile")
    <div class="smallKub"></div>
    @Html.ActionLink("პრიზები", "Index", "Prizes")
    <div class="smallKub"></div>
    @Html.ActionLink("აუქციონი", "Index", "Auction", null, new { @style = "color:red;" })
    <div class="smallKub"></div>
    @Html.ActionLink("გამარჯვებულები", "Index", "Winners")
</nav>

<div class="auqcPrize">
    <div class="prizeOverflow">
        <div class="prizePic"></div>
        <div class="prizeLeft"><p>@ViewBag.countLeft</p></div>
    </div>
    <span>Lenovo</span>
    <br />
    <span>IdeaPad G50-30</span>
    <br />
    <span>CPU: 2160</span>
    <br />
    <span>RAM: 2GB</span>
</div>
<div id="monaw">
    <span>მონაწილეები</span>
    <div id="monawline"></div>
</div>
<div id="cowndtdownDIv">
    <span>დასრულებამდე</span>
    <div id="timer"></div>
</div>

<div id="betdiv">
    @Html.Partial("Bet")
</div>

和部分观点:

@model Kubeti.Models.Codes

@if (ViewData["IsAjax"] != null && (bool)ViewData["IsAjax"] == true)
{
   ViewData["IsAjax"] = false;
}
else
{
<span>შეიყვანეთ თქვენი ფსონი</span>

using (Ajax.BeginForm("Bet", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "pozition", InsertionMode = InsertionMode.Replace }))
{
    @Html.EditorFor(x => x.code)
    @Html.ValidationMessageFor(x => x.code)

    <input type="submit" value="OK" />
}
<span id="minBet">დასადები ფსონის მინიმალური რაოდენობა: @ViewBag.startPrice</span>
<span id="pozition">თქვენი პოზიცია: </span>

}

1 个答案:

答案 0 :(得分:1)

您在部分视图中有Ajax.BeginForm。 这将导致每次调用时,表单提交,控制器输出另一个将嵌套在父表单中的表单。

通过提交包含提交按钮的每个表单,这会对您的html造成奇怪的行为。