Recently in a project on. NET, our development is based on the MVC+EF+B layer and the D layer as well as the encapsulated bottom-up approach to a framework like this. This is a file management system, no doubt, in the system, you have to log in first. So, how does mvc implement landing?
Ideas:
And ordinary landing, enter the user name and password, for the null judgment, and then call the method, and the database information to do not, if the same, the login success, the ID information to the session or cookie, other pages directly verify the session or cookie to determine whether the user login.
Realize:
As the saying goes, easy to say, hard to do. First Analyze this demand, according to my thinking, to build a controller, and then generate a view, in view to write a good page. Make a Submit button. JS writes the judgment after the commit, then Ajax calls the Controller method, validates, and then jumps to the page.
The logic is very clear and not difficult, here is my code.
View's Code
@{Layout = null;} <! DOCTYPE html>And then the code in the controller.<pre name= "code" class= "CSharp" >///<summary>/Login View//</summary>//<retu Rns></returns> public ActionResult Login () {return View (); }//<summary>//D login method Verification///</summary>//<returns>ok or Error</retur Ns> public String Confirmation () {//Gets the data returned by the foreground string userid = request["userid"]; string password = request["password"]; Declares the LOGINBLL iloginbll LOGINBLL = springhelper.getobject<iloginbll> ("LOGINBLL") in the spring container; list<userinfoviewmodel> user = Loginbll. Queryuser (userid); If the validation is passed, the data is stored in the Session, returning the successful if (user[0].pwd = = password) {session["UserID"] = Useri D session["realname"] = User[0].realname; session["JID"] = user[0].admin; return "OK"; } Validation fails, returns failure else {return ' error '; } }
This landing, can be said to be very simple, but, in the process of doing, but repeatedly error. In the process of debugging, you find that he has gone everywhere, the invocation has been successful, but, in the end, it is the return of failure. At first, I have been thinking about where the problem is, when the runtime, obviously has returned OK why or failed?Smart you, you might think, is an AJAX error. Yes, really out of Ajax, here, I say this error, but also because of this error, I added error:function () {alert ("Input exception, please re-enter!") ")},//error prompt, this sentence. This error, in fact, is very simple, if the return data type of AJAX is JSON, then, in the subsequent data reception, it is necessary to judge and parse the returned JSON data, but, my data, the obvious return is a string type, in this, we have noticed that Directly to the JSON datatype a string value, that is, datatype: ' String ', in fact, this is a misunderstanding, Ajax Datetype type is no String, and it corresponds to the text type. So, on this little point, I spent a lot of time. Take a note here and share it with you.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Mvc+ef Landing and jump to success page