Archive | Tech RSS feed for this section

DataAnnotations Custom Validation

17 Nov

I am putting this here because it took me way to long to find this information today.

Reference Links for this information:
[custom-validation-attributes]
[msdn]

To create a custom DataAnnotations validation attribute inherit from ValidationAttribute and implement IsValid.

public class CustomAttributeName : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        bool validFlag = false;
        // perform validation here
        if (validFlag)
            return true;
        else
            return false;
    }
}

In the Model Validation Meta class use the custom validation attribute like this.

[MetadataType(typeof(EntityMetadata))]
public partial class Entity
{
    public class EntityMetadata
    {
        [CustomAttributeName(ErrorMessage=" [Required] ")]
        public object FieldName { get; set; }
    }
}

Smartphone Competition

28 Oct

I watched the demo video at engadget of the new Motorola Droid phone. I must say I was pretty impressed. Droid won’t get me to switch from my iPhone for serveral reasons. I love the multimedia functionality of the iPhone, the apps and games I use, and easy sync/management with my home computer.

However there are several features of the Motorola Droid phone I would love to see on the iPhone.

  • A combined inbox view for email accounts
  • The new Google Maps for Mobile software
  • The doc and car doc accessories and the automatic mode change

If the iPhone had the new Google Maps for Mobile and a car dock I would order that right now.  The home dock for the Motorola Droid is really cool to.

I hope some competition drives Apple to continue to make the iPhone the best mobile device.

LINQ to SQL DataContext and Transactions

17 Sep

I am posting this as a way to remember it, since I am starting a real project using LINQ to SQL soon.

LINQ to SQL orders the objects with pending changes based on their dependencies. LINQ to SQL starts a transaction to wrap the SQL statements. The SQL statements are issued one by one. Any errors detected by the database cause the process to stop and all the changes are rolled back. The DataContext still contains the pending changes enabling you to try and resolve the issue and SubmitChanges again as in this example.

   1: Northwnd db = new Northwnd(@"c:\northwnd.mdf");
   2: // Make changes here. 
   3: try
   4: {
   5:     db.SubmitChanges();
   6: }
   7: catch (ChangeConflictException e)
   8: {
   9:     Console.WriteLine(e.Message);
  10:     // Make some adjustments.
  11:     // ...
  12:     // Try again.
  13:     db.SubmitChanges();
  14: }

Styling Alternating Items in ASP.NET MVC Views

8 Sep

Here are a couple of ways to apply styles to alternating items in ASP.NET MVC views.

The jQuery way:

$(’ul > li:nth-child(even)’).addClass(’alt’);
$(’ul > li:first’).addClass(’first’);
$(’ul > li:last’).addClass(’last’);

Rick Strahl’s Odd/Even Looping Method:

<%
    bool oddEven = true;
    foreach (CodeSnippetListItem snip in snippetList)
    {
%>
    <div class="<%= (oddEven = !oddEven) ? "evenclass" : "oddclass" %>">
            Content goes here...
    </div>
<% } %>

jQuery to Highlight the Current Menu Item

4 Sep

Here is a little bit of jQuery code to highlight the current menu item. This presumes you are using a UL > LI > A structure to build your menu.

<script type="text/javascript">
  $(document).ready(function() {
    var path = location.pathname;
  $("li:has(a[href$='" + path + "'])").addClass("selected");  });
</script>