Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- asp.net mvc partial view redirect or show error
- @{
- var ajaxOpts = new AjaxOptions {OnSuccess = "success"};
- }
- <div id="login">
- @Html.ValidationSummary(excludePropertyErrors: true)
- <h2>Start by Logging in</h2>
- @using (Ajax.BeginForm("LogOn", "Account", ajaxOpts))
- {
- @Html.Hidden("returnUrl", Request.Url.PathAndQuery)
- <table width="100%" border="0" cellspacing="0" cellpadding="5">
- <tr>
- <td>
- <span class="bluey">Username:</span><br />
- @Html.TextBoxFor(m => m.UserName, new {tabindex = "1", Class = "field"})
- @Html.ValidationMessageFor(m => m.UserName, "*")
- </td>
- <td>
- <span class="bluey">Password:</span><br />
- @Html.TextBoxFor(m => m.Password, new {tabindex = "2", Class = "field"})
- @Html.ValidationMessageFor(m => m.Password, "*")
- </td>
- </tr>
- <tr>
- <td>
- <input name="login" type="submit" value="Submit" class="input_btn" tabindex="3" />
- </td>
- <td>@Html.CheckBoxFor(m => m.RememberMe) @Html.LabelFor(m => m.RememberMe) <span class="bluey"> | </span> @Html.ActionLink("Forgot Password?", "Password", "User")</td>
- </tr>
- </table>
- }
- </div>
- <script>
- function success(context) {
- //var returnUrl = context.get_data().returnUrl;
- //if (returnUrl) {
- // window.location.href = returnUrl;
- //} else {
- $("#login").replaceWith(context);
- //}
- }
- </script>
- [HttpPost]
- public ActionResult LogOn(LogOnModel userDetails, string returnUrl)
- {
- if (ModelState.IsValid)
- {
- if (Membership.ValidateUser(userDetails.UserName, userDetails.Password))
- {
- FormsAuthentication.SetAuthCookie(userDetails.UserName, userDetails.RememberMe);
- return Redirect(returnUrl);
- }
- ModelState.AddModelError("", "The username or password provided was incorrect");
- }
- return PartialView(userDetails);
- }
- function success(result) {
- if (result.returnUrl) {
- // the controller action returned JSON
- window.location.href = returnUrl;
- } else {
- // the controller action returned a partial
- $('#login').html(result);
- }
- }
- return Json(new { returnUrl = returnUrl });
Add Comment
Please, Sign In to add comment