EF+MVC+Bootstrap 项目实践 Day4
一、注册账户时显示注册表单
非常简单的一句js,但平时用习惯了jquery,原生的还不会写了。。。而且Razor的转成html也折腾了下
<a class="a" href="#" onclick="document.getElementById('RegisterForm').style.display='block'">注册账户</a>
Html.BeginForm("Index","Auth",FormMethod.Post,new {@id = "RegisterForm",@style = "display:none;"})
平时要显示,就是要把css的"display:none;"改成"display:block;",都是用jquery,$("#xx").show(),最多$("#xx").css("display","block"),甚至$("#xx").attr("style","display:block;")
原生的还以为是.style="display:block;",发现不对,再试了style.display="block;",也不行,分号不能多加。
Html.BeginForm,属性参数前面要加@,这个也折腾了一会
二、验证码
源码中验证码比较复杂,有十几种样式,还有各种公共方法基类什么的,我练习就不弄这么复杂了,去掉所有未使用的(VS或Resharper有这功能,没用到的会变灰色,就可以删除)
[AuthorizeIgnore] public ActionResult VerifyImage() { var validateCodeHelper = new ValidateCodeHelper(); string code; byte[] bytes = validateCodeHelper.CreateImage(out code); //this.CookieContext.VerifyCodeGuid = VerifyCodeHelper.SaveVerifyCode(code); return File(bytes, @"image/jpeg"); }
public class ValidateCodeHelper { public Color[] DrawColors{get;set;} = { Color.FromArgb(0x6b,0x42,0x26), Color.FromArgb(0x4f,0x2f,0x4f), Color.FromArgb(50,0x99,0xcc), Color.FromArgb(0xcd,0x7f,50), Color.FromArgb(0x23,0x23,0x8e), Color.FromArgb(0x70,0xdb,0x93), Color.Red, Color.FromArgb(0xbc,0x8f,0x8e) }; private bool FontTextRenderingHint{get;set;} public int ImageHeight{get;set;} = 30; public int ValidataCodeLength{get;set;} = 4; public int ValidataCodeSize{get;set;} = 0x10; public string ValidateCodeFont{get;set;} = "Arial"; public byte[] CreateImage(out string validataCode) { Bitmap bitmap; string formatString = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"; GetRandom(formatString,ValidataCodeLength,out validataCode); MemoryStream stream = new MemoryStream(); ImageBmp(out bitmap,validataCode); bitmap.Save(stream,ImageFormat.Png); bitmap.Dispose(); stream.Close(); stream.Dispose(); return stream.GetBuffer(); } private void CreateImageBmp(ref Bitmap bitMap,string validateCode) { Graphics graphics = Graphics.FromImage(bitMap); Random random = new Random(); graphics.TextRenderingHint = FontTextRenderingHint ? TextRenderingHint.SingleBitPerPixel : TextRenderingHint.AntiAlias; Font font = new Font(ValidateCodeFont,ValidataCodeSize,FontStyle.Regular); int maxValue = Math.Max((ImageHeight - ValidataCodeSize) - 5,0); for(int i = 0; i < ValidataCodeLength; i++){ Color color = DrawColors[random.Next(DrawColors.Length)]; Brush brush = new SolidBrush(color); int[] numArray = {((i*ValidataCodeSize) + random.Next(1)) + 3,random.Next(maxValue) - 4}; Point point = new Point(numArray[0],numArray[1]); graphics.DrawString(validateCode[i].ToString(),font,brush,point); } graphics.Dispose(); } private void DisposeImageBmp(ref Bitmap bitmap) { Graphics graphics = Graphics.FromImage(bitmap); graphics.Clear(Color.White); Point[] pointArray = new Point[2]; Random random = new Random(); for(int i = 0; i < (ValidataCodeLength*2); i++){ Pen pen = new Pen(DrawColors[random.Next(DrawColors.Length)],1f); pointArray[0] = new Point(random.Next(bitmap.Width),random.Next(bitmap.Height)); pointArray[1] = new Point(random.Next(bitmap.Width),random.Next(bitmap.Height)); graphics.DrawLine(pen,pointArray[0],pointArray[1]); } graphics.Dispose(); } private static void GetRandom(string formatString,int len,out string codeString) { codeString = string.Empty; string[] strArray = formatString.Split(','); Random random = new Random(); for(int i = 0; i < len; i++){ int index = random.Next(0x186a0)%strArray.Length; codeString = codeString + strArray[index]; } } private void ImageBmp(out Bitmap bitMap,string validataCode) { int width = (int) ((ValidataCodeLength*ValidataCodeSize)*1.2); bitMap = new Bitmap(width,ImageHeight); DisposeImageBmp(ref bitMap); CreateImageBmp(ref bitMap,validataCode); } }
就是绘制随机4位英文,具体没细看,代码不算长。
三、登陆、新建
验证码还只是展示在页面上,登陆时还要对应起来,
//this.CookieContext.VerifyCodeGuid = VerifyCodeHelper.SaveVerifyCode(code);
这行注释掉的,应该就是要对照的。
四、EF
1、公共类
this.CookieContext.VerifyCodeGuid = VerifyCodeHelper.SaveVerifyCode(code);
为了这一行代码,源码引用了很多公共类(一个公共类里面有时又用到另一个公共类,只好依次都加进来),只好也在项目中加一下,以后也用的到。把不要的精简掉,也还可以。
2、生成实体
按网上学的,新建ADO.NET实体模型,有一个要注意,如果没勾选保存至web.config,生成的就叫Entities,而不是取名的AccountEntities。
3、SaveChanges()
EF也是奇葩,SaveChanges()不报错,直接就没反应了
public virtual int SaveChanges() { try { if (this.ValidateOnSaveEnabled) { IEnumerable<DbEntityValidationResult> validationErrors = this.Owner.GetValidationErrors(); if (Enumerable.Any<DbEntityValidationResult>(validationErrors)) throw new DbEntityValidationException(Strings.DbEntityValidationException_ValidationFailed, validationErrors); } return this.ObjectContext.SaveChanges((System.Data.Entity.Core.Objects.SaveOptions) (1 | (this.AutoDetectChangesEnabled && !this.ValidateOnSaveEnabled ? 2 : 0))); } catch (UpdateException ex) { throw this.WrapUpdateException(ex); } }
用Resharper看源码,是会抛出异常才对啊,自已加了try/catch
奇怪,我看了数据库时间列虽是不可空,但是有GETDATE()默认值,而且我插入了一行也正常
INSERT INTO dbo.VerifyCode ( VerifyText, Guid ) VALUES ( N'1', -- VerifyText - nvarchar(50) NEWID() -- Guid - uniqueidentifier )
如果传一个CreateTime进去,是可以保存成功,但SQL语句是不用传时间的呀
CreateTime改成可空后可以了,但不会使用其默认值GETDATE(),而是NULL
有两点明天要再搞清一下
1、为什么SaveChanges()不报错,下断点调试,不往下执行。断点如果放在下一行,就根本到不了
2、数据库中想插入默认数据要怎么办