Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 即使在关闭前一个ModelState之后,ModelState.IsValid()也不会验证_Asp.net Core_Model View Controller - Fatal编程技术网

Asp.net core 即使在关闭前一个ModelState之后,ModelState.IsValid()也不会验证

Asp.net core 即使在关闭前一个ModelState之后,ModelState.IsValid()也不会验证,asp.net-core,model-view-controller,Asp.net Core,Model View Controller,我使用的是if语句,它应该在模型状态有效时执行一些操作,但显然ModelState.IsValid不返回true。我曾尝试使用ModelState.Clear()清除以前的ModelState,但效果不佳 这是我的控制器 // This is the first action for which I'm using the model state [HttpPost] public async Task<IActionResult> Register(

我使用的是if语句,它应该在模型状态有效时执行一些操作,但显然ModelState.IsValid不返回true。我曾尝试使用ModelState.Clear()清除以前的ModelState,但效果不佳

这是我的控制器

// This is the first action for which I'm using the model state
    [HttpPost]
            public async Task<IActionResult> Register(userModel model)
            {
                if (ModelState.IsValid)
                {
                    userData lcl_userr = new userData
                    {
                        username = model.username,
                        password = model.password,
                        email = model.email
                    };
                    _context.users.Add(lcl_userr);
                    await _context.SaveChangesAsync();
                    ViewBag.Success = "Registered successfully";
                    ModelState.Clear();
                    return View();
                }
                return View(model); 
            }
    
// And this is the second one
            [HttpPost]
            public IActionResult Index(userModel model)
            {
                if (ModelState.IsValid)
                {
                    var findValue = _context.users.Any(o => o.username == model.username);
                    var findValue2 = _context.users.Any(o => o.password == model.password);
                    if (findValue && findValue2)
                        ViewBag.Name = "Success";
                }
                return View(model);
            }

第二个对
public IActionResult Index(userModel model)
的请求是
email
IsValid
为false

    [Required]
    [Display(Name = "email")]
    public string email { get; set; }
电子邮件地址的
ModelState
实例在Errors集合中有错误

当MVC为提交的属性创建模型状态时,它还会遍历
ViewModel
中的每个属性,并使用与其关联的属性验证属性。如果发现任何错误,它们将添加到属性的
ModelState
中的errors集合中

解决方案 只需在
Index
操作中reomve
if(ModelState.IsValid)
,根本不需要添加它

或者

如果您坚持保留
如果(ModelState.IsValid)
,您需要在
表单中添加
电子邮件

    <input type="hidden" asp-for="email" />

第二个对
公共IActionResult索引(userModel模型)
的请求,其模型不包含
电子邮件
IsValid
为false

    [Required]
    [Display(Name = "email")]
    public string email { get; set; }
电子邮件地址的
ModelState
实例在Errors集合中有错误

当MVC为提交的属性创建模型状态时,它还会遍历
ViewModel
中的每个属性,并使用与其关联的属性验证属性。如果发现任何错误,它们将添加到属性的
ModelState
中的errors集合中

解决方案 只需在
Index
操作中reomve
if(ModelState.IsValid)
,根本不需要添加它

或者

如果您坚持保留
如果(ModelState.IsValid)
,您需要在
表单中添加
电子邮件

    <input type="hidden" asp-for="email" />


您将验证什么?是否通过UserModel类上的数据注释进行验证?那么你的UserModel类呢。net完整框架还是.net核心?我将数据从表单发送到控制器,数据将存储在userModel类中,然后使用实体框架核心检查数据库中是否存在数据。我正在使用ASP.NET Core MVC,我已经编辑了问题并添加了userModel类。您是否使用jquery发布?不,jquery在我的代码中没有任何地方使用。您将验证什么?是否通过UserModel类上的数据注释进行验证?那么你的UserModel类呢。net完整框架还是.net核心?我将数据从表单发送到控制器,数据将存储在userModel类中,然后使用实体框架核心检查数据库中是否存在数据。我使用的是ASP.NET Core MVC,我已经编辑了问题并添加了userModel类。您是否使用jquery发布信息?不,我的代码中没有使用jquery。