zoukankan      html  css  js  c++  java
  • c# MVC 模型无法从view中获得数据

    建立一个登录Model

    public class LoginModel
    	{
    		public string name;
    		public string password;
    
    		public string login()
    		{
    			using(var db=new DataEntities())
    			{
    				var passMd5 = password.md5();
    				var user=db.Info.FirstOrDefault(u => u.userName == name);
    				if(user==null)
    				{
    					return "User name not exist";
    				}
    				if(user.passWord!=passMd5)
    				{
    					return "Password is invalid";
    				}
    				else
    				{
    					return null;
    				}
    			}
    		}
    	}
    

    在View中建立表单

    在 Controller的HttpPost method里捕捉用户登录事件,但是Model中的值均为空

    public ActionResult Login(LoginModel model)
    		{
    			var result = model.login();
    			if (result == null)
    			{
    				return RedirectToAction("Home", "User");
    			}
    			else
    			{
    				ModelState.AddModelError("",result);
    				return View(model);
    			}
    		}
    

      

    原因:

    Model里的字段必须是Property

    因此,应该是 

    public string name{get;set;}
    

      

  • 相关阅读:
    2
    1
    java10
    java8
    java9
    java7
    java6
    java5
    java4
    java3
  • 原文地址:https://www.cnblogs.com/dabaopku/p/2287534.html
Copyright © 2011-2022 走看看