Not Equal C && !=C && 不等于西
RSS icon Email icon Home icon
  • Client Side Validation For DataType Attribute in ASP.NET MVC3

    Posted on November 20th, 2012 Sean Add comments

         After 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:

         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 »

  • 1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
    Loading...Loading...
    641 views
  • MVC3 with IIS7 credential error

    Posted on March 19th, 2012 Sean Add comments

         When 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”.

  • 1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
    Loading...Loading...
    171 views
  • Custom Validation for MVC 3 Model Example 1: Positive Integer

    Posted on January 7th, 2012 Sean 2 comments

    When 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:

    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 »

  • 1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
    Loading...Loading...
    3,067 views
  • Create XML Comments for Custom Function in VIsual Studio 2010

    Posted on September 25th, 2011 Sean Add comments

         If 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.

         After finish comments, enable “XML Documentation” option for your project:

    1. Open project property
    2. Go to “Build” tab
    3. Check “Xml documentation file”
    4. Build project, then check the XML comments in other project
  • 1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
    Loading...Loading...
    418 views
  • One Script to Backup All SQL Databases

    Posted on August 15th, 2011 Sean Add comments

         It’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:

  • 1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
    Loading...Loading...
    263 views