Tuesday, January 29, 2013

NBuilder simple example in C#

Nbuilder is a great tool in developing mock builders to generate data out of you application. Assuming, you don't use database for retrieving records, you can use NBuilder with LINQ capabilities to generate test data. Assuming if you have a class in your model like this:
Code:
namespace MvcApplication1.Models  
 {  
   public class Product  
   {  
     public int Id { get; set; }  
     public string Name { get; set; }  
     public double Price { get; set; }  
     public string Description { get; set; }  
   }  
 }  
To generate a product object using NBuilder, you may use something like this:
Code:
Product product = Builder<Product>  
         .CreateNew()  
         .With(x => x.ProductId = productId)  
         .And(x => x.Price = Convert.ToDouble(productId))  
         .And(x => x.Name = "Mushroom Can")  
         .And(x => x.Description = @"Imported from New Zealand. Fresh and Juicy.")  
         .Build();
Cheers!

Monday, January 28, 2013

Debugging jQuery/Javascript Code not working in ASP.NET MVC 2 (VS 2010)

In an application where there is internal jquery code in your MVC project, placing a breakpoint won't work when you want to enable debugging mode. After searching, a workaround has been provided by Microsoft Team. The solution is to transfer the javascript/jQuery script to an external .js file instead of embedding it in your .aspx script.Refer to the workaround.
Solution:
http://connect.microsoft.com/VisualStudio/feedback/details/652428/mvc-3-mvc-2-debug-ignores-javascript-breakpoints

Cheers!
Greg

Wednesday, January 16, 2013

Position a Windows Forms MessageBox in C# (REPOST)

Here's a tip from CODE PROJECT on positioning message box. This utilize c++ dlls.
Code:
using System.Runtime.InteropServices;  
 using System.Threading;  
 [DllImport("user32.dll")]  
 static extern IntPtr FindWindow(IntPtr classname, string title); // extern method: FindWindow  
 [DllImport("user32.dll")]  
 static extern void MoveWindow(IntPtr hwnd, int X, int Y, int nWidth, int nHeight, bool rePaint);   
 // extern method: MoveWindow  
 [DllImport("user32.dll")]  
 static extern bool GetWindowRect(IntPtr hwnd, out Rectangle rect);   
 // extern method: GetWindowRect  
 void FindAndMoveMsgBox(int x, int y, bool repaint, string title)  
 {  
   Thread thr = new Thread(() => // create a new thread  
   {  
     IntPtr msgBox = IntPtr.Zero;  
     // while there's no MessageBox, FindWindow returns IntPtr.Zero  
     while ((msgBox = FindWindow(IntPtr.Zero, title)) == IntPtr.Zero) ;  
     // after the while loop, msgBox is the handle of your MessageBox  
     Rectangle r = new Rectangle();  
     GetWindowRect(msgBox, out r); // Gets the rectangle of the message box  
     MoveWindow(msgBox /* handle of the message box */, x , y,   
       r.Width - r.X /* width of originally message box */,   
       r.Height - r.Y /* height of originally message box */,   
       repaint /* if true, the message box repaints */);  
   });  
   thr.Start(); // starts the thread  
 }  


 //here's how to use it...  
 FindAndMoveMsgBox(0, 0, true,"Title");  
 MessageBox.Show("Message","Title");

Cheers!

Friday, January 11, 2013

ASP.NET MVC 404 Content Page Show's Twice

In handling 404 pages (pages that don't exist in the website),I encountered a problem wherein the asp.content tag/control would show twice in the page. Here's the server content code:
  <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">   
   <h2>Page Not Found!</h2>   
   <br />   
   Error in URL Navigation. The page you were looking for was not found.   
  </asp:Content>  
And here's the content being displayed twice.
The code I'm using was from a documentation/series on how to handle 404 pages. The tutorial has a controller factory that checks invalid routes and calls the base controller if a route is invalid. Below is the code:
Code:
   //inherit Controller Class  
   public class BaseController : Controller   
   {  
     private string context;  
     public string MyContextUrl   
     {  
       get  
       {  
         return context;  
       }  
       set  
       {  
         if (value == null)  
         {  
           throw new NullReferenceException();  
         }  
         context = value;  
       }  
     }  
     protected override void HandleUnknownAction(string actionName)  
     {  
       RouteToPage404(HttpContext);  
     }  
     public ActionResult RouteToPage404(HttpContextBase context)  
     {       
         //errorController instance  
         IController errorController = new ErrorController();  
         RouteData rd = new RouteData();  
         rd.Values.Add("controller", "Error");  
         rd.Values.Add("action", "Http404");  
         //execute controller  
         errorController.Execute(new RequestContext(context, rd));  
         return new EmptyResult();  
     }      
   }  
In-order for content to be displayed once, add checking if the controller context is not null.
Code:
       if (this.ControllerContext != null)  
       {  
         IController errorController = new ErrorController();  
         RouteData rd = new RouteData();  
         rd.Values.Add("controller", "Error");  
         rd.Values.Add("action", "Http404");  
         //execute controller  
         errorController.Execute(new RequestContext(context, rd)); 
         errorController.Execute(new RequestContext(context, rd));  
       } 
 
         return new EmptyResult();   
The 404 controller shown only once.
The controller context is not null since ErrorController inherits base controller. The controller factory in turn
does not inherit BaseController.


Greg

Wednesday, January 9, 2013

The name 'ViewData' does not exist in the current context

In an application where I played the class diagram in visual studio by adding
controller class, repository class, and service class, I encountered an error
"The name 'ViewData' does not exist in the current context" in my HomeController.

This is weird since the controller references System.Web.Mvc. The solution is:

1. Copy the source code in the HomeController and transfer it in a textfile for backup.
2. Delete the original HomeController.
3. Add another HomeController class
4. paste the backup code from the textfile.

I guess this is a bug when using class diagrams in Visual Studio 2010.

Greg