asp-for输入未绑定到ViewModel [英] asp-for inputs not binding to ViewModel

查看:105
本文介绍了asp-for输入未绑定到ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 asp-for 属性的简单表单.

I have a simple form that uses the asp-for attribute.

@using System.Threading.Tasks
@using MyApp.Controllers
@using MyApp.Models.Login
@model MyApp.Models.Login.LoginModel

<form asp-controller="Login" asp-action="Login" asp-route-returnUrl="@ViewBag.ReturnUrl" method="post" class="form-horizontal" role="form">
  <div class="form-group">
    <label class="col-md-2 control-label">UserName</label>
    <div class="col-md-6">
      <input asp-for="Username" class="form-control"/>
      <span asp-validation-for="Username" class="text-danger"></span>
    </div>
  </div>
  <div class="form-group">
    <label class="col-md-2 control-label">Password</label>
    <div class="col-md-6">
      <input asp-for="Password" type="password" class="form-control"/>
      <span asp-validation-for="Password" class="text-danger"></span>
    </div>
  </div>
  <div class="form-group">
    <div class="col-md-offset-2 col-md-1">
      <button type="submit" class="btn btn-primary">Log in</button>
    </div>
    <div class="col-md-offset-2 col-md-3">
      <a href="/ForgotPassword" class="pull-right">Forgot password?</a>
    </div>
  </div>
</form>

我的控制器有一个接受LoginModel的操作,但是当我单击提交"时,属性"为null.

My Controller has an action that accepts LoginModel, but the Properties are null when I click submit.

[AllowAnonymous]
[HttpPost("/Login")]
public async Task<IActionResult> Login(LoginModel lm, string returnUrl)
{
    //lm.Username and lm.Password are null
    //...
}

这是LoginModel:

Here is LoginModel:

public class LoginModel
{
    [Required(ErrorMessage = "Username is required", AllowEmptyStrings = false)]
    public string Username { get; set; }

    [Required(ErrorMessage = "Password is required", AllowEmptyStrings = false)]
    public string Password { get; set; }
}

我正在使用ASP.NET Core 1.0 RTM.这是我的project.json:

I'm using ASP.NET Core 1.0 RTM. Here is my project.json:

"buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
},
"dependencies": {
"EspritWeb.Services": "1.0.0-*",
"Microsoft.AspNetCore.Authentication.Cookies": "1.0.0",
"Microsoft.AspNetCore.Authorization": "1.0.0",
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Hosting": "1.0.0",
"Microsoft.AspNetCore.Hosting.Abstractions": "1.0.0",
"Microsoft.AspNetCore.Http.Extensions": "1.0.0",
"Microsoft.AspNetCore.Localization": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Routing": "1.0.0",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.AspNetCore.Session": "1.0.0",
"Microsoft.AspNetCore.StaticFiles": "1.0.0",
"Microsoft.Extensions.Caching.SqlServer": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0"
},

"frameworks": {
    "netcoreapp1.0": {
         "dependencies": {
             "Microsoft.NETCore.App": {
             "type": "platform",
             "version": "1.0.0"
             }
        }
    }
},

"runtimes": {
    "win10-x64": {}
}

我为什么会出现这种行为的任何想法?谢谢!

Any ideas why I'm getting this behavior? Thanks!

推荐答案

请确保您在Views/Shared文件夹中有一个_ViewImports.cshtml,并且在该文件中请确保您注册了taghelpers

make sure you have a _ViewImports.cshtml in the Views/Shared folder and in that file make sure you register the taghelpers

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

这篇关于asp-for输入未绑定到ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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