ArgumentNullException:值不能为null.参数名称:viewData [英] ArgumentNullException : Value cannot be null. Parameter name : viewData

查看:237
本文介绍了ArgumentNullException:值不能为null.参数名称:viewData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的登录页面,如下所示:

I have simple login page like this:

Login.cshtml

Login.cshtml

@page
@model GDPR.Views.Account.LoginModel
@{
}

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers


<html>
<body>
    <p>Login </p>
    <form method="post">
        <div asp-validation-summary="All"></div>
        Username: <input asp-for="loginData.Username" value="username" /><br />
        Password: <input asp-for="loginData.Password" value="password" /><br />
        <br />
        <input type="submit" value="Login" />
        @Html.AntiForgeryToken()
    </form>
</body>
</html>

和后面的代码:

public class LoginModel : PageModel
{
    [BindProperty] // Bind on Post
    public LoginData loginData { get; set; }

    public async Task<IActionResult> OnPostAsync()
    {
        if (ModelState.IsValid)
        {
          .......
            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
            return RedirectToPage("/Home/Index");
        }
        else
        {
            ModelState.AddModelError("", "username or password is blank");
            return Page();
        }
    }


    public class LoginData
    {
        [Required]
        public string Username { get; set; }

        [Required, DataType(DataType.Password)]
        public string Password { get; set; }
    }
}

运行该应用程序时,出现错误:ArgumentNullException值不能为null,参数名称:viewData.

When I run the app I get an error: ArgumentNullException value cannot be null, param name : viewData.

该错误的原因可能是什么?

What could be the reason for that error?

推荐答案

您在第一行上的@page为null,这是导致错误的原因.删除@page语句或使用它来定义要显示的页面.

Your @page on the first line is null which is what's causing the error. Either remove the @page statement or use it to define the page to be displayed.

这篇关于ArgumentNullException:值不能为null.参数名称:viewData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆