即使用户输入的数据没有错误,我的modelstate也总是无效的

时间:2022-09-23 20:03:06

Hi I have a method in my controller and has the following code

你好,我的控制器中有一个方法,有以下代码

if (ModelState.IsValid)

{

    IDataOperations ops = DataSession.GetDataOperations(null);
    List<Department> dept = new List<Department>();
    ops.Load(dept);
    ops.Commit();
    int deptId = dept[dept.Count - 1].Id + 1;
    Department department = new Department()
    {
      Id = deptId,
      CompanyId = deptModel.CompanyId,
      Active = deptModel.Active,
      Name = deptModel.Name
    };

    ops.Create(department);
    ops.Commit();
    return RedirectToAction("CompanyDepartment", "Task");
}

else

{
  //some code

Every time the page runs it always goes to the else part, meaning model-state is invalid. How do I check where the error in the page is, what code can I add to show where the error in the page is.

每次页面运行时,它总是转到else部分,这意味着模型-状态是无效的。如何检查页面中的错误在哪里,可以添加什么代码来显示页面中的错误在哪里。

Thank you

谢谢你!

6 个解决方案

#1


3  

You can go through the modelstate collection, by the following code, by debbuging through this, you will see, your code will go in to the for loop, and you can understand, what was the error.

你可以通过下面的代码来检查modelstate集合,通过这个代码,你会看到,你的代码会进入for循环,你可以理解,什么是错误。

#if DEBUG
       /// <summary> 
 /// Output the properties which are causing the issues when 
 /// the model is binding. 
 /// </summary>
  public static void ModelStateErrors (ModelStateDictionary modelState)
        {
            var errors = modelState.Where(a => a.Value.Errors.Count > 0)
                .Select(b => new { b.Key, b.Value.Errors })
                .ToArray();

            foreach (var modelStateErrors in errors)
            {
                 System.Diagnostics.Debug.WriteLine("...Errored When Binding.",                                  modelStateErrors.Key.ToString());

            }

        }
#endif

#2


1  

Very likely the problem is in a field in your model that isn't shown in the view. If you are performing validation, put a validation summary on the page. (Specify that field-level errors should NOT be suppressed.) That will show you all the errors including those that don't belong to a visible field.

很可能问题是在您的模型中没有显示在视图中的字段。如果您正在执行验证,请在页面上放置验证摘要。(指定字段级错误不应被抑制。)这将显示所有错误,包括不属于可见字段的错误。

#3


0  

Last time i had a problem like this it was because something was null that shouldnt be.

上次我遇到这样的问题是因为有些东西是空的,不应该是空的。

Put a breakpoint in your code on the line where you say:

在你的代码中放一个断点在你说:

   if (modelState.IsValid)

You can find which part of the code has errors by drilling down into the modelState. You should be able to work it out from there.

通过深入到modelState中,您可以找到代码中有错误的部分。你应该可以算出来。

#4


0  

You can check where the error is by doing the following.

通过执行以下操作,您可以检查错误在哪里。

var errors = ModelState.Values.SelectMany(v => v.Errors);
        if (ModelState.IsValid)
        {

        }

#5


0  

Please see this image drill down model state 1)select value 2)drill down each member and look for error

请查看此图像钻取模型状态1)选择值2)钻取每个成员并查找错误。

please find image link and see @RBT

请找到图片链接并查看@RBT

#6


-3  

Generate the field automatically when create view and copy the required fields then make some of them hidden as you want

创建视图时自动生成字段,并复制所需的字段,然后将其中一些字段隐藏起来

#1


3  

You can go through the modelstate collection, by the following code, by debbuging through this, you will see, your code will go in to the for loop, and you can understand, what was the error.

你可以通过下面的代码来检查modelstate集合,通过这个代码,你会看到,你的代码会进入for循环,你可以理解,什么是错误。

#if DEBUG
       /// <summary> 
 /// Output the properties which are causing the issues when 
 /// the model is binding. 
 /// </summary>
  public static void ModelStateErrors (ModelStateDictionary modelState)
        {
            var errors = modelState.Where(a => a.Value.Errors.Count > 0)
                .Select(b => new { b.Key, b.Value.Errors })
                .ToArray();

            foreach (var modelStateErrors in errors)
            {
                 System.Diagnostics.Debug.WriteLine("...Errored When Binding.",                                  modelStateErrors.Key.ToString());

            }

        }
#endif

#2


1  

Very likely the problem is in a field in your model that isn't shown in the view. If you are performing validation, put a validation summary on the page. (Specify that field-level errors should NOT be suppressed.) That will show you all the errors including those that don't belong to a visible field.

很可能问题是在您的模型中没有显示在视图中的字段。如果您正在执行验证,请在页面上放置验证摘要。(指定字段级错误不应被抑制。)这将显示所有错误,包括不属于可见字段的错误。

#3


0  

Last time i had a problem like this it was because something was null that shouldnt be.

上次我遇到这样的问题是因为有些东西是空的,不应该是空的。

Put a breakpoint in your code on the line where you say:

在你的代码中放一个断点在你说:

   if (modelState.IsValid)

You can find which part of the code has errors by drilling down into the modelState. You should be able to work it out from there.

通过深入到modelState中,您可以找到代码中有错误的部分。你应该可以算出来。

#4


0  

You can check where the error is by doing the following.

通过执行以下操作,您可以检查错误在哪里。

var errors = ModelState.Values.SelectMany(v => v.Errors);
        if (ModelState.IsValid)
        {

        }

#5


0  

Please see this image drill down model state 1)select value 2)drill down each member and look for error

请查看此图像钻取模型状态1)选择值2)钻取每个成员并查找错误。

please find image link and see @RBT

请找到图片链接并查看@RBT

#6


-3  

Generate the field automatically when create view and copy the required fields then make some of them hidden as you want

创建视图时自动生成字段,并复制所需的字段,然后将其中一些字段隐藏起来