本文介绍了我无法从视图中读取数据到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的登录模式。





I am having my login model.


public class Login
   {

       [Required]
       [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
       [DataType(DataType.Password)]
       [Display(Name = "Password")]
       public string password { get; set; }

       [Required(ErrorMessage = "Enter User name")]
       public string username { get; set; }
   }











当我从这里看到控制器的价值时










when i am geting value from view to controler in thisway


Login t = new Login();

           t.password = fr["password"];
           t.username = fr["username"];







但错误即将来临......




but error is coming...

password The name 'password' does not exist in the current context







username The name 'password' does not exist in the current context





我尝试过:





What I have tried:

public ActionResult Login(FormCollection fr)
        {
            //if (ModelState.IsValid)
            Login t = new Login();
       
            t.password = fr["password"];
            t.username = fr["username"];


               
                bool success = objIAccountData.Login(t);
                //var UserID = GetUserID_By_UserName(login.username);
                //var LoginType = GetRoleBy_UserID(Convert.ToString(UserID));

                if (success == true)
                {
                    if (string.IsNullOrEmpty(Convert.ToString(LoginType)))
                    {
                        ModelState.AddModelError("Error", "Rights to User are not Provide Contact to Admin");
                        return View(login);
                    }
                    else
                    {
                        Session["Name"] = login.username;
                        Session["UserID"] = UserID;
                        Session["LoginType"] = LoginType;

                        if (Roles.IsUserInRole(login.username, "Admin"))
                        {
                            return RedirectToAction("AdminDashboard", "Dashboard");
                        }




                        else
                        {
                            return RedirectToAction("UserDashboard", "Dashboard");
                        }
                    }
                }
                else
                {

                    ModelState.AddModelError("Error", "Please enter valid Username and Password");
                    return View(login);
                }
            
            //else
            //{
            //    ModelState.AddModelError("Error", "Please enter Username and Password");
            //}
            return View(login);

        } 

推荐答案


这篇关于我无法从视图中读取数据到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 10:45