Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Post
An Article
A Blog
A Video
An Ebook
An Interview Question
Register
Login
3
Reply
MVC: call ajax script after check ModelState in controller?
Fares Ayyad
7 years ago
382
Reply
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 form = $("form");
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"
);
}
});
//Ajax call
});
//savedata
});
</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.
Post
Reset
Cancel
Answers (
3
)
Next Recommended Forum
How to call Asynchronous Methods when server free?
How can I rebind the drowdownlist in Edit page MVC