我无法从视图中读取数据到控制器 [英] I am not able to read data from view to controller

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

问题描述

我的登录模式。





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);

        } 

推荐答案

您也可以分享您的观点吗?你的模型和控制器看起来很好。在fr(你的formcollection)上添加一个手表,看看那里是否真的存在用户名和密码。
Can you share your view as well ? Your model and controller appears to be fine. Add a watch on fr (your formcollection) to see if username and password are actually present there.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆