今天做个简单的登录页面,前些天用Nhibernate实体映射出了不少问题。老板有换ADO.NET ENTITY DATA MODEL 的冲动。先看看实体模型。
接着再看看控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace TestExample.Controllers
{
public class LoginController : Controller
{
//
// GET: /Login/
TestExample.Models.testEntities2 t = new Models.testEntities2();
public ActionResult Index()
{
ViewData.Model = t.SS_USER;
return View();
}
public ActionResult Login()
{
return View( "Login");
}
[HttpPost]
public ActionResult Login(FormCollection fc)
{
string userName = fc[0];
string password = fc[1];
var x = from m in t.SS_USER where m.C_USER_PW == password && m.C_USER_NM == userName select m;
if (x != null && x.ToList().Count > 0)
{
return RedirectToAction( "Index");
}
else
{
ViewData.Add( "msg", "用户名或者密码不正确");
return View( "Login");
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace TestExample.Controllers
{
public class LoginController : Controller
{
//
// GET: /Login/
TestExample.Models.testEntities2 t = new Models.testEntities2();
public ActionResult Index()
{
ViewData.Model = t.SS_USER;
return View();
}
public ActionResult Login()
{
return View( "Login");
}
[HttpPost]
public ActionResult Login(FormCollection fc)
{
string userName = fc[0];
string password = fc[1];
var x = from m in t.SS_USER where m.C_USER_PW == password && m.C_USER_NM == userName select m;
if (x != null && x.ToList().Count > 0)
{
return RedirectToAction( "Index");
}
else
{
ViewData.Add( "msg", "用户名或者密码不正确");
return View( "Login");
}
}
}
}
再看看我的View页面
<
%@ Page
Title
="" Language="C#"
MasterPageFile
="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TestExample.Models.SS_USER
>" %>
< asp:Content ID ="Content1" ContentPlaceHolderID ="TitleContent" runat ="server" >
ViewPage1
</asp:Content>
< asp:Content ID ="Content2" ContentPlaceHolderID ="MainContent" runat ="server" >
< h2 >
ViewPage1 </h2>
< script language ="javascript" type ="text/javascript" >
function validate() {
if (document.getElementById("UserName").getAttribute("value") == "") {
userValidate.innerHTML = " < font color=red >用户名不能为空 </font>";
return false;
}
else {
userValidate.innerHTML = "";
}
if (document.getElementById("Pwd").getAttribute("value") == "") {
pwdValidate.innerHTML = " < font color=red >密码不能为空 </font>";
return false;
}
else {
pwdValidate.innerHTML = "";
}
}
function validateEmpty(id, tdId) {
if (document.getElementById(id).getAttribute("value") != "") {
tdId.innerHTML = "";
}
}
</script>
<% using (Html.BeginForm("Login", "Login", FormMethod.Post))
{ %>
< table style ="width: 40%;" >
< tr >
< td >
用户名:
</td>
< td >
< %: Html.TextBoxFor(Model = > Model.C_USER_NM, new { id = "UserName", onblur = "validateEmpty('UserName',userValidate)" })%>
</td>
< td id ="userValidate" >
</td>
</tr>
< tr >
< td >
密码:
</td>
< td >
< %: Html.TextBoxFor(Model = > Model.C_USER_PW, new { id = "Pwd", onblur = "validateEmpty('Pwd',pwdValidate)" })%>
</td>
< td id ="pwdValidate" >
</td>
</tr>
< tr >
< td colspan ="3" style ="text-align: center" >
< input type ="submit" value ="登录" onclick ="return validate()" />
< input type ="reset" value ="重置" />
</td>
</tr>
< tr >
< td style ="color: Red" colspan ="3" >
< %=ViewData["msg"] == null ? "" : ViewData["msg"]% >
</td>
</tr>
</table>
< %} % >
</asp:Content>
< asp:Content ID ="Content1" ContentPlaceHolderID ="TitleContent" runat ="server" >
ViewPage1
</asp:Content>
< asp:Content ID ="Content2" ContentPlaceHolderID ="MainContent" runat ="server" >
< h2 >
ViewPage1 </h2>
< script language ="javascript" type ="text/javascript" >
function validate() {
if (document.getElementById("UserName").getAttribute("value") == "") {
userValidate.innerHTML = " < font color=red >用户名不能为空 </font>";
return false;
}
else {
userValidate.innerHTML = "";
}
if (document.getElementById("Pwd").getAttribute("value") == "") {
pwdValidate.innerHTML = " < font color=red >密码不能为空 </font>";
return false;
}
else {
pwdValidate.innerHTML = "";
}
}
function validateEmpty(id, tdId) {
if (document.getElementById(id).getAttribute("value") != "") {
tdId.innerHTML = "";
}
}
</script>
<% using (Html.BeginForm("Login", "Login", FormMethod.Post))
{ %>
< table style ="width: 40%;" >
< tr >
< td >
用户名:
</td>
< td >
< %: Html.TextBoxFor(Model = > Model.C_USER_NM, new { id = "UserName", onblur = "validateEmpty('UserName',userValidate)" })%>
</td>
< td id ="userValidate" >
</td>
</tr>
< tr >
< td >
密码:
</td>
< td >
< %: Html.TextBoxFor(Model = > Model.C_USER_PW, new { id = "Pwd", onblur = "validateEmpty('Pwd',pwdValidate)" })%>
</td>
< td id ="pwdValidate" >
</td>
</tr>
< tr >
< td colspan ="3" style ="text-align: center" >
< input type ="submit" value ="登录" onclick ="return validate()" />
< input type ="reset" value ="重置" />
</td>
</tr>
< tr >
< td style ="color: Red" colspan ="3" >
< %=ViewData["msg"] == null ? "" : ViewData["msg"]% >
</td>
</tr>
</table>
< %} % >
</asp:Content>
这里我们进行了非空验证和用户名密码正确性的验证。运行效果如下
最后我在用Nhibernate2.0.1的时候发现在执行如下的Save方法的时候如果映射文件里面包含DateTime类型,就会报日期溢出错误
public
class HibernateSave<T, PK> : IHibernateCallback
{
private T entity;
private PK pk;
public HibernateSave(T t)
{
this.entity = t;
}
public object DoInHibernate(ISession session)
{
session.FlushMode = FlushMode.Auto;
pk = (PK)session.Save(entity);
session.Flush();
return pk;
}
}
{
private T entity;
private PK pk;
public HibernateSave(T t)
{
this.entity = t;
}
public object DoInHibernate(ISession session)
{
session.FlushMode = FlushMode.Auto;
pk = (PK)session.Save(entity);
session.Flush();
return pk;
}
}
经过SqlServer自带的跟踪我发现发送给数据库服务器的SQL语句只要是DateTime类型的都带的是双引号。如下
着实很郁闷,那位仁兄如果解决过这个问题,请联系我。
本文转自 BruceAndLee 51CTO博客,原文链接:http://blog.51cto.com/leelei/339514,如需转载请自行联系原作者