【问题标题】:ASP.NET MVC form does not send data to controllerASP.NET MVC 表单不向控制器发送数据
【发布时间】:2022-01-16 01:13:07
【问题描述】:

我有一个Index.cshtml 页面,它使用 Bootstrap 显示一个通用登录页面。标记如下所示:

@model Accurecord_Direct.Models.Login.  
@{
    ViewData["Title"] = "Home Page";
    Layout = "~/Views/Shared/_NoMenu.cshtml";
}
<body>
@using (Html.BeginForm("LogIn", "Home", FormMethod.Post))
{
<section class="vh-100" style="background-color: #508bfc;">
    <div class="container py-5 h-100">
        <div class="row d-flex justify-content-center align-items-center h-100">
            <div class="col-12 col-md-8 col-lg-6 col-xl-5">
                <div class="card shadow-2-strong" style="border-radius: 1rem;">
                    <div class="card-body p-5 text-center">

                        <h3 class="mb-5">Log in</h3>

                        <div class="form-outline mb-4">
                            <input type="text" id="UserID" class="form-control form-control-lg" />
                            <label class="form-label" for="UserID">User ID</label>
                        </div>

                        <div class="form-outline mb-4">
                            <input type="password" id="Password" class="form-control form-control-lg" />
                            <label class="form-label" for="Password">Password</label>
                        </div>

                        <button class="btn btn-primary btn-lg btn-block" type="submit">Login</button>

                        <hr class="my-4">

                        <button class="btn btn-lg btn-block btn-primary" style="background-color: #dd4b39;" type="submit" asp-action="ForgotUserID"><i class="fab fa-google me-2"></i> Forgot User ID</button>
                        <button class="btn btn-lg btn-block btn-primary mb-2" style="background-color: #3b5998;" type="submit" asp-action="ForgotPassword"><i class="fab fa-facebook-f me-2"></i>Forgot Password</button>

                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
}
</body>

我的模型如下所示:

using System;  

namespace Accurecord_Direct.Models
{
    public class Login
    {
        public string UserID { get; set; }
        public string Password { get; set; }
    }
}

我的任何控制器都是这样的:

    public IActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult LogIn(IFormCollection form, [FromForm] Login newLogin)
    {
        return RedirectToAction("Index");
    }

    [HttpPost]
    [Route("ForgotUserID")]
    public IActionResult ForgotUserID()
    {
        return View("ForgotUserID");
    }

    [HttpPost]
    [Route("ForgotPassword")]
    public IActionResult ForgotPassword()
    {
        return View("ForgotPassword");
    }

我在控制器的LogIn 操作方法的返回处设置断点运行我的应用程序。当断点被命中时,我查看变量newLogin 的监视窗口。当我展开变量时,我看到了两个属性 UserIDPassword,但它们的值都是 (null)。在点击提交按钮之前,我正在表单中输入字母。

为什么不将表单数据传递给控制器​​?

谢谢!

【问题讨论】:

    标签: c# asp.net-mvc bootstrap-4


    【解决方案1】:

    修正动作

        [HttpPost]
        public ActionResult LogIn (Login newLogin)
        {
            return RedirectToAction("Index");
        }
    

    并为视图输入添加 asp-for

    <div class="form-outline mb-4">
      <input type="text" asp-for="UserId" class="form-control form-control-lg"/>
    <label class="form-label" for="UserID">User ID</label>
    </div>
     <div class="form-outline mb-4">
    <input type="password" asp-for=Password  class="form-control form-control-lg" />
     <label class="form-label" for="Password">Password</label>
     </div>
    

    【讨论】:

    • 我希望事情就这么简单。我更改了代码以符合您的建议,但 newLogin.UserID 和 newLogin.Password 的值仍然是(null)
    • @JonathanSmall 查看我的更新答案
    • 啊!旧的 asp-for。那成功了。非常感谢!
    • 不客气!
    猜你喜欢
    • 1970-01-01
    • 2016-03-23
    • 2015-11-21
    • 2020-04-17
    • 2013-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多