Note that there are some explanatory texts on larger screens.

plurals
  1. POdealing with success: and error: in an MVC3 client to server validation routine using AJAX
    primarykey
    data
    text
    <p>I am working on a validation routine that needs to go back to the server, perform a bit of database checking and then return to the client to display messages depending on the outcome.. I am using MVC3, Javascript, Ajax.</p> <p>This is my clientside:</p> <pre><code>&lt;script type="text/javascript"&gt; $('#usrid').change(function (e) { var formData = $("form").serialize(); $.ajax({ url: "ValidateOfficers", type: "POST", data: formData, success: function (data) { alert(data.reponseText); }, error: function (data) { alert(data.reponseText); } }); }); </code></pre> <p></p> <p>This is my controller action:</p> <pre><code> [HttpPost, ActionName("ValidateOfficers")] public ActionResult ValidateOfficers(userRole role) { int holderid = role.holderid; int roleSelected = role.roleid; int chosenUser = role.usrid; int countRoles = db.userRoles.Where(i =&gt; i.roleid == roleSelected &amp;&amp; i.holderid == holderid).Count(); if (countRoles == 0) //success: { Response.StatusCode = 200; Response.Write("ok"); return View("Create", role); } if (countRoles &gt; 0) //error: { Response.StatusCode = 500; Response.Write("error"); return View("Create", role); } return View(); } </code></pre> <p>What I want to do perform the count and then send back a status based on the count result.. So if countRoles = 1 then return text "officer exists" if countRoles > 1 then "duplicate records exist". if countRoles is zero then I can assume success and offer no message. </p> <p>Ok, I am stuck on how to built this custom message on the server side and how to post it back to the client. I was thinking of adding a couple of labels to the razor and show/hiding depending on the count result.. As you can see I have had a very basic go, but to no avail. I get "undefined" in the alert messages currently. </p> <p>If I can get some tips on this I would be most grateful!!</p>
    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. 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