控制器中的操作从 Html.BeginForm - ASP.NET MVC 接收到空模型

Action in controller received empty model from Html.BeginForm - ASP.NET MVC

我卡了3天了。我一直在寻找这个问题。当我提交表单时,控制器操作方法没有获取模型。

我的基本观点Login.cshtml

@{
    string durum = ViewBag.Style;

    switch (durum)
    {
        case "Login":
            Html.RenderAction("_Login", "Dashboard");
            break;
        case "LostPassword":
            Html.RenderAction("_LostPassword", "Dashboard");
            break;
        case "RegisterForm":
            Html.RenderAction("_RegisterForm", "Dashboard");
            break;
        default:
            Html.RenderAction("_Login", "Dashboard");
            break;
    }
}

我的部分观点_LostPassword.cshtml

@model HaberSitesi._Entities.Kullanici

@using (Html.BeginForm("LostPassword", "Dashboard", FormMethod.Post, new { @class = "forget-form", @style = "display:block" }))

{
    if (TempData["ForgotPassword"] != null)
    {
        <div class="alert alert-warning ">
            <button class="close" data-close="alert"></button>
            <span>@TempData["ForgotPassword"]</span>

        </div>
    }
    <h3>Şifrenizi mi unuttunuz ?</h3>
    <p> Şifrenizi almak için lütfen E-Posta adresinizi giriniz. </p>
    <div class="form-group">
        <div class="input-icon">
            <i class="fa fa-envelope"></i>
            @Html.TextBoxFor(x => x.EPosta, new { @class = "form-control placeholder-no-fix", @type = "email", @autocomplete = "off", @placeholder = "Eposta", Name = "email" })
            @Html.ValidationMessageFor(x => x.EPosta)
        </div>
    </div>
    <div class="form-actions">
        @Html.ActionLink("Geri Dön", "Login", "Dashboard", new { }, new { @type = "button", @id = "back-btn", @class = "btn grey-salsa btn-outline" })

        <button type="submit" class="btn green pull-right"> Gönder </button>
    </div>
}

控制器中的动作DashboardController.cs

 public ActionResult LostPassword()
        {
            VeriTransfer();
            return View("Login");
        }


        [HttpPost]
        public ActionResult LostPassword(Kullanici kullanici)
        {
            string kullaniciEposta = kullanici.EPosta;

            Kullanici user = _kullaniciBll.Get(kullaniciEposta);

            if (user != null)
            {
                TempData["ForgotPassword"] = "Şifreniz e-posta adresinize gönderildi.";
            }
            else
            {
                TempData["ForgotPassword"] = "Kayıtlarımızda e-posta adresiniz bulunamadı";
            }
            VeriTransfer();
            return View("Login");
        }

当我点击提交按钮时,我无法在控制器中获取任何数据 (Kullanici kullanici)。每个 属性 来自模型的空值或默认数据值。

注意:也许我的代码可能还有一些与我的问题无关的其他错误。我只是想知道为什么我得到空模型。谢谢,至少你已经阅读了我的问题。

您的 属性 名为 EPosta 但您将其名称更改为 Name="email"。因此当 POST 操作发生时它会发送一个 属性调用 email 到控制器操作,您的 Kullanici 对象需要一个名为 EPosta

的 属性

这两个都可以解决您的问题:

  1. Name="email"更改为Name="EPosta"
  2. 完全删除Name="email"

但是就像 Stephen 说的那样,最好将其完全删除,因为如果您将来将 属性 重命名为 EPosta2 而忘记将名称更改为 Name="EPosta2",您的 POST 将不再有效