Neo
Neo

Reputation: 16239

How do I show error message separately in angular js in mvc cshtml page?

I have implemented server side validation in mvc project.

controller code

   return Json(ListErrors, JsonRequestBehavior.AllowGet);

create.js

     .error(function (responseText, status, error) {
                                $scope.errorvalue = false;
                                $scope.errors = responseText;
   $('#txtname').addClass('errorClass');
                $scope.name = responseText;
                    $scope.namealert = true;
                            });

In responseText I'm getting list of errors with property name and error message , enter image description here

How can I bind error message to different labels based on property name?

If property name is Name then error message should be please enter name for name input textbox

I tried like following but it gives all object value :(

  <input id="txtname" type=text ng-model="Name" class="form-control">
  <label ng-model="name" ng-show="namealert" style="color:red;margin-top:-35px">{{name}}</label>

output

[{"PropertyName":"Name","ErrorMessage":"Please enter Namename","AttemptedValue":null,"CustomState":null,"ErrorCode":null,"FormattedMessageArguments":null,"FormattedMessagePlaceholderValues":null}]

Upvotes: 0

Views: 507

Answers (1)

SRSonnenwald
SRSonnenwald

Reputation: 301

So you are close. You have your JSON that contains the key/value pairs that you would like to bind. In your controller parse the JSON to get the values out and set the value you get out to a property on the viewmodal that you have bind to a label control.

Here is a website with an example: https://ujjaini.wordpress.com/2014/06/09/binding-json-data-to-front-end-angular-js-framework/

Upvotes: 1

Related Questions