sitBy
sitBy

Reputation: 269

MVC Partial view appearing in another window

If the user clicks on register the first time it opens up the popup window like it should. However if the user clicks on submit, it opens a new window, instead of staying on the same page and displaying the popup again.

How it should be: What happens when user doesn't enter anything:

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

Controller Code:

 // 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>

Upvotes: 2

Views: 1931

Answers (1)

MrDeveloper
MrDeveloper

Reputation: 1041

Inside of your controller, I believe that instead of just

return Register();

You'll need

return RedirectToAction("Register");

Upvotes: 1

Related Questions