通过弹出窗口注册并在此弹出窗口中传递结果

时间:2012-01-27 04:49:16

标签: javascript asp.net-mvc jquery-ui

我实现了注册并通过弹出窗口(Jquery UI Dialog)登录。 这是我的UserNavigation控件

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<script type="text/javascript">
    $(document).ready(function () {
        $("#loginDialog").click(function () {
            $.get("/Account/LogOn",
                function (htmlResult) {
                    $("#logon").remove();
                    $("#container").append(htmlResult);
                    $("#logon").dialog({ modal: true });
                }
                    );
            return false;
        });

        $("#registerDialog").click(function () {
            $.get("/Account/Register",
                function (htmlResult) {
                    $("#registration").remove();
                    $("#container").append(htmlResult);
                    $("#registration").dialog({ modal: true });
                }
                    );
            return false;
        });
    });
</script>

<div id="userNavigation">
    <%
    if (Request.IsAuthenticated)
    {
    %>
    <a href="/users/mr.freeman/profile" class="username"><span><%=Page.User.Identity.Name %></span><i class="icon iUser"></i></a>
    <ul class="headLine_link">
        <li><a href="#">Profile settings</a></li>
        <li>
            <%= Html.ActionLink("Log off", "LogOff", "Account", null, new { @class = "exit" })%></li>
    </ul>
    <%
    }
    else
    {%>    
    <ul class="headLine_link">
                    <li><%=Html.ActionLink("Register", "Register", "Account", null, new {id = "registerDialog"})%></li>
                    <li><%=Html.ActionLink("Log On", "LogOn", "Account", null, new {id = "loginDialog", @class = "username" })%></li>
                </ul>
    <%}%>
</div>

它运作正常。但是,我希望当注册成功时,然后在同一窗口中显示消息。我怎么能这样做?

这是我的控制者:

[HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus;
                Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    //FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

并且,第二个问题:如果用户在需要授权的站点上执行某些操作,则显示登录屏幕(如果用户未经授权)。

我尝试使用Authorize属性,但不起作用。

感谢。

更新:注册视图:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<DT.KazBilet.Web.Portal.Models.RegisterModel>" %>
<div id="registration">
    <h2>Create new account</h2>


    <script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
    <script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>" type="text/javascript"></script>

    <% using (Html.BeginForm()) { %>
        <%: Html.ValidationSummary(true, "Error. Try again.") %>
        <div>
            <fieldset>
                <legend>Account Information</legend>

                <div class="editor-label">
                    <%: Html.LabelFor(m => m.UserName) %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(m => m.UserName) %>
                    <%: Html.ValidationMessageFor(m => m.UserName) %>
                </div>

                <div class="editor-label">
                    <%: Html.LabelFor(m => m.Email) %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(m => m.Email) %>
                    <%: Html.ValidationMessageFor(m => m.Email) %>
                </div>

                <div class="editor-label">
                    <%: Html.LabelFor(m => m.Password) %>
                </div>
                <div class="editor-field">
                    <%: Html.PasswordFor(m => m.Password) %>
                    <%: Html.ValidationMessageFor(m => m.Password) %>
                </div>

                <div class="editor-label">
                    <%: Html.LabelFor(m => m.ConfirmPassword) %>
                </div>
                <div class="editor-field">
                    <%: Html.PasswordFor(m => m.ConfirmPassword) %>
                    <%: Html.ValidationMessageFor(m => m.ConfirmPassword) %>
                </div>

                <p>
                    <input type="submit" value="Register" />
                </p>
            </fieldset>
        </div>
    <% } %>
</div>

1 个答案:

答案 0 :(得分:0)

Html.BeginForm()更改为Ajax.BeginForm(),然后响应将在弹出窗口中呈现,而不是在整个页面中呈现。

从estetic的角度来看,您还应该将Html.ValidationSummary移出注册表单,因为它会随着Html.ValidationMessageFor一起更新错误,并且您不需要将这些信息加倍,最好的是在形式之前放置它。 Html.ValidationSummary应该用于显示控制器的错误。