MVC部分视图出现在另一个窗口中

时间:2015-06-23 12:39:53

标签: asp.net-mvc post

如果用户在第一次打开弹出窗口时点击注册就像应该的那样。但是,如果用户点击提交,它会打开一个新窗口,而不是停留在同一页面上并再次显示弹出窗口。

它应该如何: 当用户没有输入任何内容时会发生什么:

_Layout Code:

<section id="login">
                    @Ajax.ActionLink("Register", "../Users/Register",
                                      new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "user-popup", InsertionMode = InsertionMode.Replace })
                </section>
                <div id="user-popup"></div>

控制器代码:

 // GET: Users/Register
    public PartialViewResult Register()
    {
        return PartialView("_Register");
    }

    // POST: Users/Register
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Register([Bind(Include = "Username,Email,Password")] User user)
    {
        if (ModelState.IsValid)
        {
            db.Users.Add(user);
            db.SaveChanges();
            return RedirectToAction("../Home/Index");
        }

        return Register();
    }

_Register code:

 @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        <div class="col-md-12">
            @Html.EditorFor(model => model.Username, new { htmlAttributes = new { @class = "form-control", @placeholder = "Username" } })
            @Html.ValidationMessageFor(model => model.Username, "", new { @class = "text-danger" })
        </div>
    </div>

1 个答案:

答案 0 :(得分:1)

在你的控制器内部,我相信而不仅仅是

return Register();

你需要

return RedirectToAction("Register");