0

I have a complication to display a ValidationMessageFor or mvc span, I have a ajax method to check for an id bd and it returns a Boolean me, this result if there want this.

function ExistProduct() {

    $.ajax({
        type: 'GET',
        url: "/Products/ExistProduct",
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify({ "id": id }),
        datatype: "json",
        success: function (data) {
            if (data.exist == true) {
                // show ValidationMessageFor 
            }
            else {

            }
        }
    });
}

as I can resolve this and not allow submit apply and save

1
  • Are you wanting to validate that a product exists? If so then use the RemoteAttribute How to: Implement Remote Validation in ASP.NET MVC. Dont try and reinvent the wheel Commented Apr 20, 2016 at 4:14

2 Answers 2

2

Your Function

function ExistProduct() {

    $.ajax({
        type: 'GET',
        url: "/Products/ExistProduct",
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify({ "id": id }),
        datatype: "json",
        success: function (data) {
            if (data.exist == true) {
                // show ValidationMessageFor 
                jQuery("#valodation_msg").css({"color":"green"});
                jQuery("#valodation_msg").html("Success");
            }
            else {
                jQuery("#valodation_msg").css({"color":"red"});
                jQuery("#valodation_msg").html("Error");
                return false;
            }
        }
    });
}

Add One Div to your HTML Code Where you want to show validation msg

<div id="valodation_msg"></div>
Sign up to request clarification or add additional context in comments.

4 Comments

hello thanks, but to not allow submit, is that possible?
if I submit this is my code ` @Html.EditorFor(model => model.ProductID, new { htmlAttributes = new { id="txtProductoid",@class = "form-control"} }) @Html.ValidationMessageFor(model => model.ProductID, "", new { @class = "text-danger" }) <div id="valodation_msg"></div>`
OH... Can You Explain What You Want Exactly?
ok, I need that when spam is displayed for any errors that the process is similar like ValidationMessageFor, do not accept the submit until the error is resolved
0
function ExistProduct() { 

 $.ajax({
    type: 'GET',
    url: "/Products/ExistProduct",
    contentType: "application/json;charset=utf-8",
    data: JSON.stringify({ "id": id }),
    datatype: "json",
    success: function (data) {
        if (data.length > 0) {
            // show ValidationMessageFor 
        var p = document.createElement('p'),
        txt = document.createTextNode("Your validation message");
        p.appendChild(txt);
        document.getElementById("id of the element where u want to  show validation message ").appendChild(p);
        return false;

        }
        else {

        }
    }
});

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.