Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear All,

I am trying to post an MVC page using AjAX and this page (view) is 'strong typed' and its model has all the data annotation.

On posting the page, JS validation has not been raised and 'ModelState.IsValid' always equal to TRUE.


Note: I am not posting the entire page, I am just posting some fields to the action.


So can you help if there is a way to fire JS validation?



Thanks

What I have tried:

I just made sure that my model has all the required data annotations and all the JS script files are included in my bundle.

$.ajax({
            url: "@Url.Action("XXX")",
            type: "post",           
            data: {
                __RequestVerificationToken: $("[name='__RequestVerificationToken']").val(),
                id: $("#Id").val(),
                name: $("#Name").val(),
                remarks: $("#Description").val(),
                level: $("#level").val(),
                status: $("#Status").val(),
                memebers: x
            },
            datatype: "json",
            success: function (data) {
                alert(data.msg);
                if (data.result)
                { window.location.href = "@Url.Action("Index")";}
            },
            error: function (data)
            { $("#loading").hide();  alert("Erorr while processing your request"); },
            complete: function (data)
            { $("#loading").hide(); }
        });
Posted
Updated 5-Jun-19 2:19am
v4
Comments
F-ES Sitecore 5-Jun-19 6:28am    
It depends on how you are submitting the page and what you're using for js validation. It's impossible to give specific help when you've given no real details.
Mina Nady Sarofem 5-Jun-19 8:00am    
Hello F-ES Sitecore,

i am posting using the follow code:

$.ajax({
url: "@Url.Action("XXX")",
type: "post",
data: {
__RequestVerificationToken: $("[name='__RequestVerificationToken']").val(),
id: $("#Id").val(),
name: $("#Name").val(),
remarks: $("#Description").val(),
level: $("#level").val(),
status: $("#Status").val(),
memebers: x
},
datatype: "json",
success: function (data) {
alert(data.msg);
if (data.result)
{ window.location.href = "@Url.Action("Index")";}
},
error: function (data)
{ $("#loading").hide(); alert("Erorr while processing your request"); },
complete: function (data)
{ $("#loading").hide(); }
});
ZurdoDev 5-Jun-19 10:06am    
Google how to manually fire the MVC validation because you are not posting the page back, you'll have to do it manually.
Mina Nady Sarofem 8-Jun-19 5:39am    
Hi,

Do you mean that I need to write JS function to check/validate on the entries ? and that MVC auto validation works only when i post the entire page ?
Please let me know if i got you.

Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900