0
我有一個使用asp-for
屬性的簡單表單。asp-for輸入不綁定到ViewModel
@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,但當我單擊提交時,屬性爲空。
[AllowAnonymous]
[HttpPost("/Login")]
public async Task<IActionResult> Login(LoginModel lm, string returnUrl)
{
//lm.Username and lm.Password are null
//...
}
這裏是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 1.0的核心RTM。這是我的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": {}
}
任何想法,爲什麼我得到這種行爲?謝謝!
你已經註冊了_ViewImports.cshtml的taghelpers? @addTagHelper *,Microsoft.AspNetCore.Mvc.TagHelpers –
@JoeAudette做到了!謝謝!!! –
太棒了,我已將我的評論發佈爲答案,以便您可以接受答案 –