模型值为空

时间:2019-01-07 17:37:17

标签: c# asp.net-core razor-pages

我正在尝试发送一些表单值,但它们似乎为空。我不知道为什么。我肯定这是一个简单的解决方案,或者只是我错过的事情。 有人可以指出正确的方向吗?

@page
@model WebApp.Pages.LoginModel
@{
    ViewBag.Title = "Login";
}
<h2>
    Logins
</h2>
@using (Html.BeginForm())
{

    @Html.ValidationSummary(true)

    <div style="border: solid 1px #ccc; padding: 30px 0 30px 30px; border-radius: 5px;
        width: 325px; margin: auto; display: table;">
        <table>
            <tr>
                <td valign="top">
                    @Html.LabelFor(model => model.UserName)
                </td>
                <td>
                    @Html.EditorFor(model => model.UserName)
                    <div style="height: 20px;">
                        @Html.ValidationMessageFor(model => model.UserName)
                </div>
            </td>
        </tr>
        <tr>
            <td valign="top">
                @Html.LabelFor(model => model.Password)
            </td>
            <td>
                @Html.EditorFor(model => model.Password)
                <div style="height: 20px;">
                    @Html.ValidationMessageFor(model => model.Password)
            </div>
        </td>
    </tr>
    <tr>
        <td></td>
        <td>
            <input type="submit" value="Login" />
        </td>
    </tr>
</table>
</div>
}

这是我的页面模型

namespace WebApp.Pages
{
    public class LoginModel : PageModel
    {

        [Required(ErrorMessage = "Please enter user name.")]
        [Display(Name = "User Name")]
        public string UserName { get; set; }

        [Required(ErrorMessage = "Please enter password.")]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }


        private SqlEntreprenörData<Entreprenör> _repository;

        public LoginModel(SqlEntreprenörData<Entreprenör> repository)
        {
            _repository = repository;
        }

        [HttpPost]
        public ActionResult OnPost()
        {
            if (ModelState.IsValid)
            {
                if(_repository.Login(UserName, Password))
                {

                   return RedirectToPage("/Admin/Avbokningar");
                }
            }
            return Page();
        }
    }
}

这是我从数据库获取用户的方法。从razorpage发送的用户名和密码在这里始终为空吗?

public bool Login(string username, string password)
{
   var user = (from userlist in _dbContext.Entreprenör
                where userlist.Namn == username && userlist.Password == password
                select new
                {
                    userlist.Id,
                    userlist.Namn
                }).ToList();
    if (user.FirstOrDefault() != null)
    {
        return true;
    }
    else
    {
        return false;
    }

}

1 个答案:

答案 0 :(得分:1)

[BindProperty] 属性在Razor页面中用于将页面中的值绑定到PageModel属性。 尝试以下更改

    [BindProperty]
    [Required(ErrorMessage = "Please enter user name.")]
    [Display(Name = "User Name")]
    public string UserName { get; set; }

    [BindProperty]
    [Required(ErrorMessage = "Please enter password.")]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

从ASP.NET Core 2.1版开始,您可以将新的 [BindProperties] 属性添加到PageModel类,而不是将 [BindPRoperty] 应用于各个属性,导致PageModel中的所有公共属性都参与了模型绑定:

[BindProperties]
public class LoginModel : PageModel
{

    [Required(ErrorMessage = "Please enter user name.")]
    [Display(Name = "User Name")]
    public string UserName { get; set; }

    [Required(ErrorMessage = "Please enter password.")]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }