小编use*_*551的帖子

MVC 条件必填字段

在我的程序中,对于一个实体,我有一个保存和一个提交按钮。对于我的实体,我有一个字段来确定是否不需要另一个字段。这种情况的一个例子:

public class Question{
bool IsRequired {get; set;}
string Answer {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

我知道我可以根据 IsRequired 字段创建一个自定义验证属性来使 Answer 成为必需,但问题是,我只需要在用户“提交”表单时进行此验证,而不仅仅是保存它。
验证实体服务器端的最佳方法是什么?我正在考虑在我的服务类中创建一个 IsValid 方法并返回一个错误列表,然后将其添加到控制器中的 ModelState 中。或者,也许使用自定义验证属性并在客户端执行,并在单击保存时以某种方式禁用验证,并为提交按钮启用它。似乎应该有一个更优雅的解决方案。有没有人有什么建议?

编辑:这是我用于我的属性的代码:

 public class RequiredIfAttribute : ValidationAttribute
    {

        RequiredAttribute _innerAttribute = new RequiredAttribute();
        private string _dependentProperty { get; set; }
        private object _targetValue { get; set; }

        public RequiredIfAttribute(string dependentProperty, object targetValue)
        {
            this._dependentProperty = dependentProperty;
            this._targetValue = targetValue;
        }
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var field = validationContext.ObjectType.GetProperty(_dependentProperty);
            if (field != null)
            {
                var …
Run Code Online (Sandbox Code Playgroud)

asp.net validation asp.net-mvc

2
推荐指数
1
解决办法
5813
查看次数

Chrome系统调用等待异步结果

我正在开发Chrome扩展程序,该扩展程序将从Chrome.System apis收集信息,并立即使用所有数据.问题当然是这些调用是异步的.我没有很多关于JS的经验,所以我想确保我以最简单的方式做到这一点.
我能想到的唯一方法是在回调中创建嵌套函数.

就像是:

chrome.identity.getProfileUserInfo(function(userinfo){
          getLocalIPs(userinfo.email, function(email, ips){
//keep passing data and nesting here.... and then do something after all calls are made
}
}
Run Code Online (Sandbox Code Playgroud)

这似乎很难很快地阅读代码.做这样的事情的推荐方法是什么.使用同步编程,我想要完成这样的事情:

var email = getEmail();
var ip = getIP();
var processor = getProcessor();

dosomething(email, ip, processor);  
Run Code Online (Sandbox Code Playgroud)

javascript asynchronous google-chrome-extension

0
推荐指数
1
解决办法
341
查看次数