0

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}]
5
  • 1
    Why don't you use an ng-repeat over responseText (which seems to be an object and not a string/text, so the variable name is a bit off) or extract the first items error message using responseText[0].ErrorMessage? Commented Jun 2, 2015 at 17:13
  • I'm very new to angular and I want to extract error messages using propertyName I'm confused how Name textbox can bind Name property error message? Commented Jun 2, 2015 at 17:14
  • What do you mean with "using propertyName"? What value do you expect to be in your input, and why, and where does it come from? Commented Jun 2, 2015 at 17:22
  • 1
    Btw, your <label> should have the for attribute to refer to your input, <label for="txtname"></label> if you want them to be related to each other Commented Jun 2, 2015 at 17:23
  • I mean to say if I have two input one is for Name other is for Description now When I got two errors like please enter name and please enter description I should apply name error to name textbox. and description error message to description textbox but i;m failed to display messages with different textbox I can display all messages at one place using MyErrors in errors thats is ` $scope.errors = responseText;` Commented Jun 2, 2015 at 17:28

1 Answer 1

1

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/

Sign up to request clarification or add additional context in comments.

2 Comments

thanks how may I convert it in my above code if possible jsfiddle will be helpful as i;m new to angular thanks :)

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.