【发布时间】:2016-06-12 07:43:52
【问题描述】:
我已经坚持了 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)。每个属性都来自模型的 null 或默认数据值。
注意:也许我的代码可能有一些与我的问题无关的其他错误。我只是想知道为什么我得到空模型。谢谢,至少你已经阅读了我的问题。
【问题讨论】:
-
删除
new { Name = "email" }永远不要尝试覆盖HtmlHelper方法生成的name属性 -
这对我有帮助,谢谢。
标签: asp.net-mvc controller html.beginform