Asp.net mvc ASP.NET MVC回发和HtmlHelper控件未反映模型更改

Asp.net mvc ASP.NET MVC回发和HtmlHelper控件未反映模型更改,asp.net-mvc,asp.net-mvc-4,razor,Asp.net Mvc,Asp.net Mvc 4,Razor,我面临MVC5 Razor web应用程序的问题。我有一个身份验证页面cshtml,其中包含Id和密码帮助器控件: @model NetInfinity.Middleware.VistaModelos.LoginVistaModelo @using (Html.BeginForm()) { @Html.AntiForgeryToken() <h1>@Login.Acceso</h1> <p>

我面临MVC5 Razor web应用程序的问题。我有一个身份验证页面cshtml,其中包含Id和密码帮助器控件:

@model NetInfinity.Middleware.VistaModelos.LoginVistaModelo
@using (Html.BeginForm())
{
        @Html.AntiForgeryToken()
        <h1>@Login.Acceso</h1>
        <p>
            @Html.TextBoxFor(c => c.Id, new { @placeholder = @Login.Usuario, autofocus = "", autocomplete = "off", maxlength = "15", size = "15" })
        </p>
        <p class="p1">
            @Html.PasswordFor(c => c.Clave, new { @placeholder = @Login.Contraseña, maxlength = "20", size = "20" })
            @Html.ActionLink(".", "Cambiopwd", null, new { @class = "login-cambiarpwd", id = "Cambiopwd" })
        </p>
        <p class="login-recordarpwd">@Html.ActionLink(@Login.RecordarPwd, "Recordatoriopwd")</p>
        <button type="button" class="login-submit" id="login-submit">@Login.LoginSubmit</button>
}
验证用户的控制器操作是:

[HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(LoginVistaModelo vmUsuario)
        {
            if (ModelState.IsValid)
            {
                EntidadesBD backend;
                var cache = MemoryCache.Default;
                backend = (EntidadesBD)cache.Get("backend");

                if (backend == null)
                {
                    backend = new EntidadesBD();
                    var politica = new CacheItemPolicy { Priority = CacheItemPriority.NotRemovable };
                    cache.Set("backend", backend, politica);
                }

                Usuario usuario = vmUsuario.ValidaUsuario();

                if (usuario == null)
                {
                    vmUsuario.MensajeError = "error2";
                    vmUsuario.Id = vmUsuario.Clave = String.Empty; // <--- This not works
                    ModelState.Clear(); // <-- This not works
                }
                else
                {
                }
            }

            return View(vmUsuario);
        }
当触发登录操作以验证用户和密码并抛出错误时,我需要清除textboxforvalue和passwordforvalue,为了实现这一点,我在控制器中将模型属性Id和Clave设置为string.empty,但是当再次呈现页面cshtml时,控件保留旧值,忽略模型更改,即使ModelState.Clear也不行。我听说像.TextBoxFor等HtmlHelpers控件不绑定回发上的模型值,而是直接从ModelState的后缓冲区中获取它们的值。请问,在模型属性中更改控件时,如何更新控件值


谢谢

解决此类问题的更好方法是重定向用户,而不是返回视图。否则,您会遇到一个问题,即如果他们按F5,它将重新发布数据。因此,只需重定向用户,并使用TempData包含错误消息。在Get方法中,检查TempData是否包含错误消息,如果包含,则显示错误消息。

在返回查看之前,请尝试将model的值设为null,
比如vmUsuario.id=null,vmUsuario.clave=null;然后返回空模型查看

我不清楚您想做什么。当出现错误时,为什么要清除这些值?更好的可用性是保留这些值,这样用户就可以看到他们犯了什么错误。否则他们可能会继续犯同样的错误。因为我想强迫用户重新提供用户和密码,这是非常糟糕的可用性。尤其是当你想到使用触摸设备(如手机)的用户时。在输入用户名时,可能需要花费相当大的努力才能将多个键盘转换为不同的数字和字符。我绝对讨厌这样的网站。解决方案是面向内部网,而不是设备或电话。
[HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(LoginVistaModelo vmUsuario)
        {
            if (ModelState.IsValid)
            {
                EntidadesBD backend;
                var cache = MemoryCache.Default;
                backend = (EntidadesBD)cache.Get("backend");

                if (backend == null)
                {
                    backend = new EntidadesBD();
                    var politica = new CacheItemPolicy { Priority = CacheItemPriority.NotRemovable };
                    cache.Set("backend", backend, politica);
                }

                Usuario usuario = vmUsuario.ValidaUsuario();

                if (usuario == null)
                {
                    vmUsuario.MensajeError = "error2";
                    vmUsuario.Id = vmUsuario.Clave = String.Empty; // <--- This not works
                    ModelState.Clear(); // <-- This not works
                }
                else
                {
                }
            }

            return View(vmUsuario);
        }