0
我意識到註冊並通過彈出窗口(JQuery UI對話框)登錄。 這是我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="https://stackoverflow.com/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
屬性,但不行。
謝謝。
UPDATE:註冊查看:
<%@ 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>