TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Fares Ayyad
NA
235
74.6k
MVC: call ajax script after check ModelState in controller?
Mar 7 2017 8:44 AM
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.
Reply
Answers (
3
)
How to call Asynchronous Methods when server free?
How can I rebind the drowdownlist in Edit page MVC