Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using MVC 3 you can set an onclick event for your Login button and then send and ajax POST to the logon action. Have the Logon action return a JSON result and control where the user is sent from your javascript function. </p> <pre><code>[HttpPost] public JsonResult LogOn(LogOnModel model, string returnUrl) { if (ModelState.IsValid) { //Do your authentication FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); return Json(true); } // If we got this far, something failed, redisplay form return Json(false); } </code></pre> <p>In your View add an Id to your form and put a click handler on the button.</p> <pre><code>&lt;% using (Html.BeginForm("LogOn", "Account", FormMethod.Post, new { Id = "frmLogOn" })) { %&gt; &lt;%: Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.")%&gt; &lt;div&gt; &lt;fieldset&gt; &lt;legend&gt;Account Information&lt;/legend&gt; &lt;div class="editor-label"&gt; &lt;%: Html.LabelFor(m =&gt; m.UserName)%&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;%: Html.TextBoxFor(m =&gt; m.UserName)%&gt; &lt;%: Html.ValidationMessageFor(m =&gt; m.UserName)%&gt; &lt;/div&gt; &lt;div class="editor-label"&gt; &lt;%: Html.LabelFor(m =&gt; m.Password)%&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;%: Html.PasswordFor(m =&gt; m.Password)%&gt; &lt;%: Html.ValidationMessageFor(m =&gt; m.Password)%&gt; &lt;/div&gt; &lt;div class="editor-label"&gt; &lt;%: Html.CheckBoxFor(m =&gt; m.RememberMe)%&gt; &lt;%: Html.LabelFor(m =&gt; m.RememberMe)%&gt; &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Log On" onclick="clicked(); return false;" /&gt; &lt;/p&gt; &lt;/fieldset&gt; &lt;/div&gt; &lt;% } %&gt; &lt;script type="text/javascript"&gt; function clicked() { var form = $('#frmLogOn'); $.ajax({ type: 'POST', url: '/Account/LogOn', data: form.serializeObject(), success: function (result) { if (result == true) { alert("success"); window.top.location = "/Home/Index"; } else { alert("failure"); } }, error: function (data) { alert("error"); } }); } &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload