简体   繁体   English

空字段,但不为空

[英]Null field, but not null

So I've got the following lines of code: 所以我有以下几行代码:

else
            {
                //if not found, call Gateway Add()
                user.Id = await C3SDbContext.UserGateway.NextIdAsync(context);

                user.CreatedById = modifier.CreatedById;
                user.CreatedBy = modifier.CreatedBy;
                user.DateCreated = DateTime.Now;
                user.UserType = "G";
                System.Diagnostics.Debug.WriteLine(user.UserType);
                user.Status = UserStatus.NEW;
                System.Diagnostics.Debug.WriteLine(user.UserType);

                user.Uic = await C3SDbContext.UicGateway.GetUicByIdAsync(context, user.UicId);
                System.Diagnostics.Debug.WriteLine(user.UserType);
                user.Role = await C3SDbContext.RoleGateway.GetRoleByIdAsync(context, user.RoleId);
                System.Diagnostics.Debug.WriteLine(user.UserType);
                if (ModelState.IsValid)
                {
                    userCheck = await C3SDbContext.UserGateway.AddNewGovernmentUserAsync(context, user, modifier);
                }
                else
                {

                    System.Diagnostics.Debug.WriteLine(user.UserType);
                    ICollection<ModelState> ListValues = ModelState.Values;
                    List<object> Errors = new List<object>();
                    foreach (var item in ModelState.Values)
                    {
                        if (item.Errors.Count() > 0)
                        {
                            Errors.Add(item.Errors);
                        }
                    }
                }
            }

user is an instance of GovernmentUser.cs, which inherits from User.cs. 用户是GovernmentUser.cs的实例,该实例继承自User.cs。 UserType is a string property of User. UserType是User的字符串属性。 When I run this, all the instances of "System.Diagnostic.Debug.WriteLine(user.UserType);" 当我运行此命令时,“ System.Diagnostic.Debug.WriteLine(user.UserType);”的所有实例 return "G" in the Output window. 在“输出”窗口中返回“ G”。 Heowever, the Errors list returns one item, telling me that UserType is null. 但是,错误列表将返回一项,告诉我UserType为null。

My questions are: what is going on here? 我的问题是:这是怎么回事? How can they both come to different results, when executed at the same type, and how can I get it so that ModelState.IsValid == true? 当它们以相同的类型执行时,如何都能得到不同的结果?如何使ModelState.IsValid == true?

ModelState checks the data that was posted to you in MVC. ModelState检查在MVC中发布给您的数据。 I don't know if this is in your controller or not, but if it is, then my guess is that the UserType wouldn't be on the original data that was posted. 我不知道这是否在您的控制器中,但是如果是,那么我的猜测是UserType不会在发布的原始数据上。 I don't think you can use that check after setting it server-side. 我认为您无法在服务器端设置它后使用该检查。 It is a check on the data received from the client. 这是对从客户端收到的数据的检查。 If UserType isn't required on the client side, just remove that rule from the Dto. 如果客户端不需要UserType,只需从Dto中删除该规则。 However, if you are uploading the actual Entity directly here and it is using the Required attribute that EF uses, then I would just save it like normal and let EF handle the valdiation instead of using ModelState. 但是,如果您直接在此处上传实际实体,并且使用的是EF使用的Required属性,那么我将像正常情况一样保存它,让EF处理审定而不是使用ModelState。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM