我已经在mvc项目中实现了服务器端验证。
Controller 代码
return Json(ListErrors, JsonRequestBehavior.AllowGet);
创建.js
.error(function (responseText, status, error) {
$scope.errorvalue = false;
$scope.errors = responseText;
$('#txtname').addClass('errorClass');
$scope.name = responseText;
$scope.namealert = true;
});
在responseText中,我收到了属性名称
和错误消息
的错误列表,
如何根据属性名称
将错误消息
绑定(bind)到不同的标签
?
如果属性名称为名称
,则错误消息应为请输入名称
对于名称
输入文本框
我尝试了以下操作,但它给出了所有对象值:(
<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>
输出
[{"PropertyName":"Name","ErrorMessage":"Please enter Namename","AttemptedValue":null,"CustomState":null,"ErrorCode":null,"FormattedMessageArguments":null,"FormattedMessagePlaceholderValues":null}]
最佳答案
所以你很接近。您的 JSON 包含您想要绑定(bind)的键/值对。在 Controller 中解析 JSON 以获取值,并将获取的值设置为已绑定(bind)到标签控件的 View 模式上的属性。
这是一个带有示例的网站:https://ujjaini.wordpress.com/2014/06/09/binding-json-data-to-front-end-angular-js-framework/
关于javascript - 如何在 mvc cshtml 页面中的 Angular js 中单独显示错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30601742/