-
Client Side Validation For DataType Attribute in ASP.NET MVC3
Posted on November 20th, 2012 Add commentsAfter Microsoft announced ADO.NET Entity Framework 4.1, database design becomes to very easy via Code First approach. And the DataType Attribute from “System.ComponentModel.DataAnnotations” namespace has been used very popular during Code First. Here are some examples:
12345678public class User{[DataType(DataType.EmailAddress)]public string UserEmail { get; set; }[DataType(DataType.Date)]public DateTime CreateDate { get; set; }}But be noticed, this validation of attribute only fires when you check “ModelState.IsValid” property in Controller or calling “SaveChanges()” in Entity Context, not in the client side. fortunately, JQuery has all the client side validation for those data types. So, the lazy way is just connect them together.
Read the rest of this entry »
641 views
-
MVC3 with IIS7 credential error
Posted on March 19th, 2012 Add commentsWhen I deploy my new MVC 3 project to a Windows 2008 R2 with IIS 7 server, I got an error with “401 – Unauthorized: Access is denied due to invalid credentials” on every page.
After some google’s, I figure out the reason was: in my “_layout” page, I have the code “@Html.RenderAction(…)”, and the action I was trying to render actually is under “Home” controller with “[Authorize]” attribute. In another word, when IIS calling MVC pages, ASP.NET will ask for authentication, and if not authenticated, it’s supposed to redirect to “_logon” page, but unfortunately the “_logon” page (which is using “_layout” as master page of cause) also asked for authentication because of rendering action. It became to a dead end.
Based on the reason explained above, the fastest solution is remove the “[Authorize]” attribute from the controller. But if you don’t want change your code, go to IIS management tool, select the website your are running, choose “Error Pages”, and “Edit Feature Settings”, change “Error Responses” to “Detailed errors”.
171 views
-
Custom Validation for MVC 3 Model Example 1: Positive Integer
Posted on January 7th, 2012 2 commentsWhen you define a property of the model, you can always put some MS’s attributes to validate the data, like “Required”, “Range”. Also you can use custom validation via expression or creating new attribute class. For example, the “NumberOfStock” for “Product” must be an integer and positive.
Actually to accomplish this validation is quite easy, if use regular expression:12345678public class Product{[Required(ErrorMessage = "Number of Stock is Required")][RegularExpression("^\\d+$", ErrorMessage = "Must be a Positive Non-decimal Number")]public int NumberOfStock{ get; set; }/******** Other Properties **************/}The way showed above will be good enough if you only have a few models or a small application. For a big project, you might want to create your own attribute instead of using expression. The advantage of dong this is easy to centralize (if you have many custom validations), and easy to debug.
Read the rest of this entry »
3,067 views
-
Create XML Comments for Custom Function in VIsual Studio 2010
Posted on September 25th, 2011 Add commentsIf you need to add your custom comments for your function, including function summary, parameter description, return value etc., just add “///” right before your function, then VS will generate XML layout for you.
123456789/// <summary>////// </summary>/// <param name="para"></param>/// <returns></returns>public bool YourFunction(int para){//Code here}After finish comments, enable “XML Documentation” option for your project:
- Open project property
- Go to “Build” tab
- Check “Xml documentation file”
- Build project, then check the XML comments in other project
418 views
-
One Script to Backup All SQL Databases
Posted on August 15th, 2011 Add commentsIt’s a nightmare for backup your DB if you have 100+ DB’s in SQL server. Just simply run a script while you taking a coffee:
12345678910111213141516171819202122232425262728DECLARE @name VARCHAR(50) -- database nameDECLARE @path VARCHAR(256) -- path for backup filesDECLARE @fileName VARCHAR(256) -- filename for backupDECLARE @fileDate VARCHAR(20) -- used for file nameSET @path = "C:\Backup"SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)DECLARE db_cursor CURSOR FORSELECT nameFROM master.dbo.sysdatabasesWHERE name NOT IN ('master','model','msdb','tempdb')--skip system database (optional)OPEN db_cursorFETCH NEXT FROM db_cursor INTO @nameWHILE @@FETCH_STATUS = 0BEGINSET @fileName = @path + @name + '_' + @fileDate + '.BAK'BACKUP DATABASE @name TO DISK = @fileNameFETCH NEXT FROM db_cursor INTO @nameENDCLOSE db_cursorDEALLOCATE db_cursor
263 views
Recent Comments