简体   繁体   English

登录失败后将用户名传递回MVC视图/文本框

[英]Passing username back to to MVC view/textbox after unsuccessful login

On an unsuccessful login, I'd like to retain the username field. 如果登录失败,我想保留用户名字段。 I've got an ViewModel object called "user" that contains the string Username 我有一个名为“ user”的ViewModel对象,其中包含字符串Username

public class User
{
   public bool RememberMe { get; set; }
   public string Username { get; set; }
}

That information is passed up to the controller method via 该信息通过以下方式传递给控制器​​方法

login( User user )
{
    … validate user
    if (!valid)
    {
        ModelState.AddModelError("", "Login name or password is incorrect!");
        return View(user);  
    }
}

I validated that the username does exist in the user object being passed back to the view. 我验证了用户名确实存在于传递回视图的用户对象中。

Now in the view I have this: 现在在视图中我有这个:

@model Myapp.Models.User

<label class="form-label" for="form-signin-username">Username</label>
<input data-bind="value: loginViewModel.Username" class="form-control" id="form-signin-username" name="username" type="text" required="" placeholder="Username">

The javascript is this: JavaScript是这样的:

var LoginViewModel = function () {
    self.UserName = ko.observable();
    self.Password = ko.observable();
    self.Authenticate = function (event) {
        var data = {
            UserName: self.UserName(),
            Password: self.Password()
        }
        xhr = $.ajax({
            data: data,
            dataType: 'JSON',
            type: "POST",
            url: "/Home/Login",
            success: function (data) {
            if (data.Success) {
            // process successful login ( this works)
            }
            else
            {
                // this is what doesn't work
                self.UserName = data.Username;  
            }
        }
    }
}

Can anyone see why that textbox doesn't autofill with the previous username? 谁能看到为什么该文本框没有自动填充以前的用户名?

You would need to make sure the desired model values are assigned to the view model 您需要确保将所需的模型值分配给视图模型

For example 例如

var LoginViewModel = function () {
    self.UserName = ko.observable(@(Model.Username)); //set initial value
    //...

I would also suggest using the HtmlHelper for the inputs in the view 我也建议对视图中的输入使用HtmlHelper

@model Myapp.Models.User

<!--...-->

@Html.LabelFor(m => m.Username, "Username")
@Html.TextBoxFor(m => m.Username, new { data_bind = "value: Username", @class = "form-control", placeholder="Username"})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 渲染默认视图后,AspNet MVC使我弹回登录 - AspNet MVC bounces me back to login AFTER rendering default view 值是文本框在返回查看后消失 - Value is textbox disappears after it comes back to view 创建帐户重定向后,MVC中的登录控件未显示用户名 - Login control in MVC not displaying username after account creation redirect 在没有模型MVC4的情况下将值从文本框传递到部分视图 - Passing value from textbox to partial view without model MVC4 ASP MVC将视图中的模型数据作为表单数据传递回 - ASP MVC passing model data in view back as form data MVC视图未将模型传递回控制器,奇怪的错误? - MVC View not passing back model to controller, weird error? 在asp.net mvc中注册成功后用户名自动填写登录页面(重定向到登录) - Username auto fill in Login Page after Register success (Redirect to Login) in asp.net mvc 登录用户名和文本框文本未保存在数据库中 - Login Username and Textbox text not saving in the database 在执行表单操作后将参数传递回视图吗? - Passing a parameter back to a view after performing a form action? 将文本框的值从View传递到Controller中的方法/操作,然后将数据从响应传递回表单 - Passing a value of a textbox from View to a method/action in Controller and then data from response back to form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM