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

时间:2016-06-12 07:43:53

标签: asp.net-mvc controller html.beginform

我已经坚持了3天。我一直在寻找这个问题。当我提交表单时,Controller操作方法不会获得模型。

我的基本视图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)。每个属性都是null或模型中的默认数据值。

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

1 个答案:

答案 0 :(得分:0)

您的属性名为EPosta,但您将其名称更改为Name="email"。因此,当POST操作发生时,它会向控制器操作发送一个名为email的属性,并且您Kullanici 1}} object需要一个名为EPosta

的属性

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

  1. Name="email"更改为Name="EPosta"
  2. 完全删除Name="email"
  3. 但是,斯蒂芬表示最好完全删除它,因为如果您将来将您的属性重命名为EPosta2而忘记将名称更改为Name="EPosta2",您的POST将不再有效