You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET MVC中如何将用户名和密码传递到控制器方法

登录页面参数无法传递到控制器的解决方案

你的问题出在两个关键细节上,以下是具体问题和修复方案:

问题1:Input标签的name属性为空

ASP.NET Core的模型绑定依赖表单元素的name属性匹配控制器参数。你手动给输入框设置了name="",这会覆盖asp-for自动生成的正确name值,导致表单提交时不会携带用户名和密码的键值对,控制器的userName和userPassword参数自然接收不到值。

问题2:提交按钮嵌套在标签内

你把提交按钮放在了<a>标签里,这个<a>会生成指向Login方法的GET请求链接,点击提交按钮时可能优先触发<a>的跳转行为,而非表单的POST提交,进一步导致参数无法正确传递。


修改后的HTML代码

<div class="login-box">
    <h2>Login</h2>
    <form id="AccountLogin" method="post" asp-controller="Accounts" asp-action="Login">
        <div asp-validation-summary="All" class="text-danger"></div>
        <div class="user-box">
            <!-- 移除name="",asp-for会自动生成正确的name属性 -->
            <input id="uName" asp-for="UserName" type="text" required="">
            <label>Username</label>
            <span asp-validation-for="UserName" class="text-danger"></span>
        </div>
        <div class="user-box">
            <!-- 移除name="",asp-for会自动生成正确的name属性 -->
            <input id="pWord" asp-for="UserPassword" type="password" required="">
            <label>Password</label>
            <span asp-validation-for="UserPassword" class="text-danger"></span>
        </div>
        <!-- 移除包裹提交按钮的<a>标签,直接保留提交按钮 -->
        <input type="submit" value="Login" class="btn text-light" 
               style="opacity: 1; border:0; font-size:16px; text-transform:uppercase; letter-spacing: 2px"/>
    </form>
</div>

额外优化建议(可选)

为了更规范地处理表单数据,建议创建登录视图模型:

public class LoginViewModel
{
    [Required]
    public string UserName { get; set; }
    
    [Required]
    public string UserPassword { get; set; }
    
    public bool RememberLogin { get; set; }
}

然后修改控制器方法接收该模型:

public async Task<IActionResult> Login(LoginViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = users.Where(x => x.UserName == model.UserName && x.UserPassword == model.UserPassword).FirstOrDefault();
        if (user == null)
        {
            ViewBag.Message = "Invalid Credential";
            return View(model);
        }
        else
        {  
            var claims = new List<Claim>() {
                new Claim(ClaimTypes.NameIdentifier, Convert.ToString(user.UserId)),
                new Claim(ClaimTypes.Name, user.UserName),
                new Claim("FavoriteDrink", "Tea")
            };   
            var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);  
            var principal = new ClaimsPrincipal(identity);

            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, new AuthenticationProperties()
            {
                IsPersistent = user.RememberLogin
            });
            return LocalRedirect(user.ReturnUrl);
        }
    }
    return View(model);
}

内容的提问来源于stack exchange,提问作者AlexJames

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 13:15:32