Simple MVC AJAX GET Method

MVC makes doing AJAX calls simple.  As I have said before, I do not like the AJAX controls and methods that the Microsoft ships with the MVC platform.  I prefer to create my own AJAX methods instead of posting a AJAX form.  I find I can get better control and I can create tighter rules around elements of importance.

The one thing I learned early is when doing a Get Method, you need to ensure that you give permission to the AJAX call to return JSON by adding the following to your JSON return object:

JsonRequestBehavior.AllowGet

This is an easy example of how to use your basic jQuery with your MVC Controller.

Continue reading Simple MVC AJAX GET Method

Joined the Dark Side and Loving It

A while back now I had a friendly debate with a colleague. It began innocently in a pub on a Friday night and it was Web Forms vrs MVC in the DotNet framework. I was anti-MVC for various reasons and in some sense I could still make an argument against MVC. We escalated our debate to a more public forum where we took the topic to the masses and presented our two sides. We created the exact same projects in our chosen DotNet platforms ( me in Web Forms and he was in MVC ) and we presented the projects. We talked about what we thought were the plus sides of what we chose and why. Before the debate I posted a question on LinkedIn for feedback and the feedback seemed positive for Web Forms. At the end of the debate, we asked our audience to make a choice based on their experience and what they had seen and Web Forms won.
Continue reading Joined the Dark Side and Loving It

The Story of C# Meeting AngularJS

Most of you already know that I still like my WebForms. People have argued that using AngularJS with WebForms is pointless, but I say NA! It makes the user experience a little bit smoother.

In this blog I will show you how to create a table that can be sorted and paged using AngularJS. I chose not to use a webserivce as I did not feel like this warranted the extra overhead and code. I wanted to keep this absolutely simple. I would have used a webserivce had I been creating an API for others to use. This is also a project that is ultimately using WebForms, had I been doing an MVC project; webservices would have been a better choice.

Is this the best way; probably not! This is my first run with this, so I am doing it the way I know and the way I find the easiest to manipulate for my purpose.
Continue reading The Story of C# Meeting AngularJS

Starting a WebForm Project

    I started working in the DotNet while it was in the Beta stage. Ewe Beta! Since then I discovered there is more than one way to tackle a start of a project and I have perfected it for my purpose.

    I would suggest starting with an empty project rather than letting Microsoft build you one. By you creating an empty project you are able to control what goes in it. The “Web Forms”template that Microsoft offers has so many files and structure that you would likely not even use.
    Continue reading Starting a WebForm Project

How a Developer can Mitigate Stress of a Designer

Working with a designer and can be a challenge as a developer as we are totally different mind sets.  Working with a designer as a DotNet developer can be even more of a challenge, but there can be a simpler way.  DotNet can be absolutely daunting to a designer who is used to working with HTML or PHP, but using your designer as you build your application will make things easier for everyone.

If I have my way; I like to work with the design first and then do my coding as this provides the best results and the least stress for everyone involved.

Continue reading How a Developer can Mitigate Stress of a Designer

DotNet Postbacks Not Working in IE11

Many people are posting the woes of DotNet Postbacks not working in IE11.  I too experienced this and remembered that there were app_browser files that could be modified to give the browser further instructions.  With a quick google search I was able to find the exact solution that I was looking for.  I cannot take credit for it so I will share the original URL from which I got my answer. [ Answer ]

However Here is the code so you do not have to click on the link above:

 <browsers>  
   <browser id="IE11" parentID="Mozilla">  
     <identification>  
       <userAgent match="Trident\/7.0; rv:(?'version'(?'major'\d+)(\.(?'minor'\d+)?)(?'letters'\w*))(?'extra'[^)]*)" />  
       <userAgent nonMatch="IEMobile" />  
     </identification>  
     <capture>  
       <userAgent match="Trident/(?'layoutVersion'\d+)" />  
     </capture>  
     <capabilities>  
       <capability name="browser" value="IE" />  
       <capability name="layoutEngine" value="Trident" />  
       <capability name="layoutEngineVersion" value="${layoutVersion}" />  
       <capability name="extra" value="${extra}" />  
       <capability name="isColor" value="true" />  
       <capability name="letters" value="${letters}" />  
       <capability name="majorversion" value="${major}" />  
       <capability name="minorversion" value="${minor}" />  
       <capability name="screenBitDepth" value="8" />  
       <capability name="type" value="IE${major}" />  
       <capability name="version" value="${version}" />  
     </capabilities>  
   </browser>  
   <!-- Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11,0) like Gecko -->  
   <browser id="IE110" parentID="IE11">  
     <identification>  
       <capability name="majorversion" match="11" />  
     </identification>  
     <capabilities>  
       <capability name="ecmascriptversion" value="3.0" />  
       <capability name="jscriptversion" value="5.6" />  
       <capability name="javascript" value="true" />  
       <capability name="javascriptversion" value="1.5" />  
       <capability name="msdomversion" value="${majorversion}.${minorversion}" />  
       <capability name="w3cdomversion" value="1.0" />  
       <capability name="ExchangeOmaSupported" value="true" />  
       <capability name="activexcontrols" value="true" />  
       <capability name="backgroundsounds" value="true" />  
       <capability name="cookies" value="true" />  
       <capability name="frames" value="true" />  
       <capability name="javaapplets" value="true" />  
       <capability name="supportsCallback" value="true" />  
       <capability name="supportsFileUpload" value="true" />  
       <capability name="supportsMultilineTextBoxDisplay" value="true" />  
       <capability name="supportsMaintainScrollPositionOnPostback" value="true" />  
       <capability name="supportsVCard" value="true" />  
       <capability name="supportsXmlHttp" value="true" />  
       <capability name="tables" value="true" />  
       <capability name="supportsAccessKeyAttribute" value="true" />  
       <capability name="tagwriter" value="System.Web.UI.HtmlTextWriter" />  
       <capability name="vbscript" value="true" />  
     </capabilities>  
   </browser>  
 </browsers>  

How to change IE11 Useragent

I recently tripped on a need to change the Useragent string for IE11 because of an older DotNet control that I was using and was no longer in development. Again this is not my solution, but it worked for me and I thought I would share it with the world. Since it is not my solution, here is the original link [ Answer ]

 <rewrite>  
 <rules>  
 <rule name="Change IE 11 User Agent to IE 10" enabled="true">  
  <match url="(.*)" />  
  <action type="None" />  
  <conditions>  
   <add input="{HTTP_USER_AGENT}" pattern=".trident\/[789]" />  
  </conditions>  
  <serverVariables>  
   <set name="HTTP_USER_AGENT" value="Mozilla/5.0 (compatible; MSIE 11.0; Windows NT 6.2; Trident/7.0)" />  
  </serverVariables>  
  </rule>  
  </rules>  
 </rewrite>  

To make this work, you need to add the variable “HTTP_USER_AGENT” to the rewrite module in IIS.  If you do not, you will be told to by the error returned in IIS.  For instructions for adding the variable click the following:  [ link ]

IE11 Thinking it is a Cookieless Browser

Recently I had an issue where IE11 did not want to track cookies turning IE11 into a Cookieless Browser.  I learned quickly this is the default behaviour of the browser unless you uncheck a no-tracking option during its setup.  You know that you have an issue when you have a url that has hash like injection in it after a user signs in.  Since you cannot force your users to turn off the no-tracking option, here is a way I got around it for sites with logins.

In the code below, the key is cookieless=”UseCookies”

 <authentication mode="Forms">  
  <forms name=".AUTH" cookieless="UseCookies" loginUrl="/" timeout="10000" path="/" />  
 </authentication>  

Building a C-Sharp Class

I have created this example of a C# class to demonstrate one way of creating a basic class cbject representing one employee.  This class could be used as a starting point for just about any object.

Take note that there are no business rules in this example.  It is of my opinion that you create a basic object first and create a business class second which extends the basic object.  In the business class you may put an EmployeeNumber generator, MaxLength controls on the fields, Phone number validation and other rules that may apply based on your business practices.  A business class will be demonstrated in a later blog.

You may also want to create a collection class.  A collection class or a manager class would manage a collection of objects with Add, Remove, Select, Sort type functions.  This too will be demonstrated in a later blog.

    /// <summary>
    /// Class Object Representing Employee
    /// </summary>
    public class Employee : Dictionary<String, Object>, IEmployee
    {
        // ***********************| Properties |***********************
        /// <summary>
        /// Employee Number
        /// </summary>
        public String EmployeeNumber
        {
            get { return (String)this[GetName()]; }
            set { this[GetName()] = value; }
        }

        /// <summary>
        /// Employee's First Name
        /// </summary>
        public String FirstName
        {
            get { return (String)this[GetName()]; }
            set { this[GetName()] = value; }
        }

        /// <summary>
        /// Employee's Last Name
        /// </summary>
        public String LastName
        {
            get { return (String)this[GetName()]; }
            set { this[GetName()] = value; }
        }
        /// <summary>
        /// Employee's Address Line 1
        /// </summary>
        public String Address1
        {
            get { return (String)this[GetName()]; }
            set { this[GetName()] = value; }
        }

        /// <summary>
        /// Employee's Address Line 2
        /// </summary>
        public String Address2
        {
            get { return (String)this[GetName()]; }
            set { this[GetName()] = value; }
        }

        /// <summary>
        /// Employee's City
        /// </summary>
        public String City
        {
            get { return (String)this[GetName()]; }
            set { this[GetName()] = value; }
        }

        /// <summary>
        /// Employee's Province / State
        /// </summary>
        public String Province
        {
            get { return (String)this[GetName()]; }
            set { this[GetName()] = value; }
        }

        /// <summary>
        /// Employee's Country
        /// </summary>
        public String Country
        {
            get { return (String)this[GetName()]; }
            set { this[GetName()] = value; }
        }

        /// <summary>
        /// Employee's ZIP / Postal Code
        /// </summary>
        public String ZipPostal
        {
            get { return (String)this[GetName()]; }
            set { this[GetName()] = value; }
        }

        /// <summary>
        /// Employee's Home Phone Number
        /// </summary>
        public String HomePhoneNumber
        {
            get { return (String)this[GetName()]; }
            set { this[GetName()] = value; }
        }

        /// <summary>
        /// Employee's Cell Phone Number
        /// </summary>
        public String CellPhoneNumber
        {
            get { return (String)this[GetName()]; }
            set { this[GetName()] = value; }
        }

        /// <summary>
        /// Employee's Full Name ( ReadOnly )
        /// </summary>
        public String FullName
        {
            get { return String.Format("{0}, {1}", FirstName, LastName); }
        }

        /// <summary>
        /// Employee's Start Date
        /// </summary>
        public DateTime StartDate
        {
            get { return (DateTime)this[GetName()]; }
            set { this[GetName()] = value; }
        }

        /// <summary>
        /// Employee's TerminationDate ( Nullable )
        /// </summary>
        public DateTime? TerminationDate
        {
            get { return (DateTime?)this[GetName()]; }
            set { this[GetName()] = value; }
        }
        // ***********************| Constructor(s) |***********************
        /// <summary>
        /// Constructor
        /// </summary>
        public Employee()
        {
            Initialize();
        }

        /// <summary>
        /// Employee
        /// </summary>
        /// <param name="employeeNumber">Initial Employee Number</param>
        public Employee(String employeeNumber)
        {
            Initialize();
            EmployeeNumber = employeeNumber;
        }
        // ***********************| Methods |***********************

        /// <summary>
        /// Initialize the Class
        /// </summary>
        private void Initialize()
        {
            EmployeeNumber = "";
            FirstName = "";
            LastName = "";
            Address1 = "";
            Address2 = "";
            City = "";
            Province = "";
            Country = "";
            ZipPostal = "";
            HomePhoneNumber = "";
            CellPhoneNumber = "";
        }

        /// <summary>
        /// Get Method Name
        /// </summary>
        /// <returns>First and Last Name</returns>
        public String GetName()
        {
            StackTrace stackTrace = new StackTrace();
            StackFrame stackFrame = stackTrace.GetFrame(1);
            MethodBase methodBase = stackFrame.GetMethod();
            return methodBase.Name.Replace("set_", "").Replace("get_", "");
        }

        /// <summary>
        /// Re-initializes the Class
        /// </summary>
        public void Reset()
        {
            this.Clear();
            Initialize();
        }
    }

Shortcut To Creating Properties in C-Sharp – Revisited

Based on my earlier an post, a I had a question about my technique and if there was a benefit, or could you use MethodBase.GetCurrentMethod().Name. Truthfully I did not know the answer until I tried it. I found this new way works, but I also find it is a little tougher to look at style-wise. You need to use stack frames if you nest the routine like I had in my original post.

public class ErrorLogRecord
    {
        public Hashtable _hsh = new Hashtable();

        public DateTime DateOfOccurance
        {
            get { return (DateTime?)_hsh[MethodBase.GetCurrentMethod().Name.Replace("set_", "").Replace("get_", "")] ?? DateTime.Now; }
            set { _hsh[MethodBase.GetCurrentMethod().Name.Replace("set_", "").Replace("get_", "")] = value; }
        }
        public String ErrorText
        {
            get { return (String)_hsh[MethodBase.GetCurrentMethod().Name.Replace("set_", "").Replace("get_", "")]; }
            set { _hsh[MethodBase.GetCurrentMethod().Name.Replace("set_", "").Replace("get_", "")] = value; }
        }
    }

Original Referenec Shortcut To Creating Properties in C-Sharp