Hello if i wnat to call the following script after check the ModelState property in the controller in order to ensure validation, how can i implment this :
- <script>
- $(document).ready(function () {
-
- $("#savedata").on("click", function (evt) {
- evt.preventDefault();
-
-
-
- var data = {
- Name: $("#userid").val(),
- Password: $("#password").val()
- }
- $.ajax({
-
- url: "/api/login",
- type: "POST",
- data: JSON.stringify(data),
- contentType: "application/json",
- success: function () {
- alert("Success");
-
- },
- error: function () {
- alert("Fail");
- }
- });
-
-
-
- });
-
- });
- </script>
The url is calling an api that will check if the usernam and password matches those in database.
the view also contatin beginForm that will be associated to action in controller to check ModelState:
- @using (Html.BeginForm("LoginProcess", "Login"))
- {
- <div class="container">
- <div class="row">
- <div class="col-md-6 col-md-offset-3 alert alert-warning">
- <div class="row">
- <div class="col-md-12">
- <div class="form-group">
- @Html.LabelFor(c => c.Customers.Name)
- @Html.TextBoxFor(c => c.Customers.Name, new { @class = "form-control" })
- @Html.ValidationMessageFor(c => c.Customers.Name)
- </div>
- <div class="form-group">
- @Html.LabelFor(c => c.Customers.Password)
- @Html.TextBoxFor(c => c.Customers.Password, new { @class = "form-control" })
- @Html.ValidationMessageFor(c => c.Customers.Password)
- </div>
- @Html.AntiForgeryToken()
- <button value="Submit" class="btn btn-primary">Save</button>
- </div>
- </div>
- </div>
- </div>
- </div>
-
- }
What i want again is to check ModelState to validate the two textBoxes then if it evaluates to true then run the script.