2016-04-20 2 views
-1

Ich habe eine Komplikation, um eine ValidationMessageFor oder mvc span anzuzeigen, habe ich eine Ajax-Methode, um eine ID bd zu überprüfen, und es gibt eine Boolean mich, dieses Ergebnis, wenn dies gewünscht wird.Wie Anzeige ValidationMessageFor von js

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 { 

      } 
     } 
    }); 
} 

wie ich dieses Problem lösen kann und nicht anwenden lassen und senden

+0

Möchten Sie bestätigen, dass ein Produkt existiert? Wenn dies der Fall ist, verwenden Sie das 'RemoteAttribute' [Anleitung: Implementieren der Remote-Überprüfung in ASP.NET MVC] (https://msdn.microsoft.com/en-us/library/gg508808 (VS.98) .aspx). Versuchen Sie nicht, das Rad neu zu erfinden –

Antwort

0

Ihre Funktion

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; 
      } 
     } 
    }); 
} 

hinzufügen Ein Div zu Ihrem HTML-Code speichern Wenn Sie die Validierung msg

<div id="valodation_msg"></div> 
zeigen wollen
+0

hallo danke, aber nicht zu erlauben, ist das möglich? –

+0

ja .. füge "return false;" unter Fehler msg. –

+0

Wenn ich das einreiche ist das mein Code '@ Html.EditorFor (model => model.ProductID, neu {htmlAttributes = new {id =" txtProductoid ", @ class =" form-control "}}) @ Html.ValidationMessageFor (model => model.ProductID, "", new {@class = "text-danger"})

' –

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 { 

     } 
    } 
}); 

}