Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you really want to pass your user name and password to a server controller and get result in client script, you can do like this</p> <p>Have an action method in your Account Controller to validate your username password combination like this. You will return <code>Json</code> from the action method based on the validation you do in your method. We declare a class called "<code>JsonDataResult</code>" which you can use for the jSon communication. This class has two properties.a string property called "<code>Message</code>" where you can send a message("Success"/"Errors") and List of strings to store the error messages if any.</p> <pre><code>public class JsonDataResult { public string Message { get;set;} public List&lt;String&gt; Items = new List&lt;String&gt;(); } </code></pre> <p>and here is my Action method looks like</p> <pre><code> public ActionResult Logon(string userName,string password) { // check your username password and if there is its not valid, do this List&lt;String&gt; errors = new List&lt;String&gt;(); errors.Add("username is not available"); errors.Add("password is not nice!"); var data = new JsonDataResult { Message = "Errors", Items = errors }; return Json(data); } </code></pre> <p>You can Set the Value of Message as "Success" if the username and password is correct. You can skip adding the error messages also.</p> <p>Now, from your client script, use jQuery post to post data to an Action Method and receive the jSon result and check the Message properties value. If it says "Errors", loop thru each errors and show it.</p> <pre><code>$(function () { $("form").submit(function () { $.post('@Url.Action("Logon","Account")', { userName: $("#username").val(), password: $("#password").val() }, function (data) { if (data.Message == "Errors") { $.each(data.Items, function (index) { alert(data.Items[index]); }); } else { // validation is successful.May be you want to redirect now ? } }); return false; }); }); </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.
    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