Showing posts with label MVC. Show all posts
Showing posts with label MVC. Show all posts

Thursday, 31 May 2012

Unit testing MVC routes

If you are using the MVC Routing engine, you can test your routes to ensure that they work as expected.

To test the default route:

[Test]
routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home",
          action = "Index",
          id = UrlParameter.Optional }
);

The example test is:

[Test]
public void DefaultRouteTest()
{
    var routes = new RouteCollection();
    var application = new Application();
    application.RegisterRoutes(routes);

    var context = new Mock<httpcontextbase>();
    context.Setup(p => p.Request.AppRelativeCurrentExecutionFilePath)
                                   .Returns("~/");
    var routeData = routes.GetRouteData(context.Object);

    Assert.AreEqual(((Route)routeData.Route).Url, 
                    "{controller}/{action}/{id}");
    Assert.AreEqual("Home", routeData.Values["controller"]);
    Assert.AreEqual("Index", routeData.Values["action"]);
}

This came from an original post by Scott Gu, that we had to change slightly. http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx

The original code was causing the first parameter to drop the first character. i.e. For the default route, the controller was return 'ome' instead of 'home'.

Wednesday, 11 April 2012

Datatables update function shows an Alert Box

When using the jQuery Datatable plugin and jeditable to allow tables to be editable, calling the update function via sUpdateUrl shows an alert box on it's return.
To suppress the alert box, return the same value that was originally passed in.
C# MVC Example:
$('#myDataTable').dataTable().makeEditable({
    sUpdateURL: "/Home/UpdateData"
})

public class HomeController : Controller
{
    public string UpdateData(int id, string value,
                             int? rowId, int? columnPosition,
                             int? columnId, string columnName)
    {
        //Add code to update cell
        return value;
    }
}
Code borrowed from http://code.google.com/p/jquery-datatables-editable/wiki/EditCell

Tuesday, 6 March 2012

Checkbox validation with the MVC data annotations

I've been using MVC3 for a new site and require a check box to be true before continuing. e.g. "Accept T & Cs".

The data annotation have the [Required] attribute for other field types, but this is ignored for check boxes.

You can create your own custom validators. Below is an example for a single check box to ensure it is true.

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class MustBeTrueAttribute : ValidationAttribute
{
      public override bool IsValid(object value)
      {
            return value != null && value is bool && (bool)value;
      }
}

You can then add the attribute to your MVC component model

[Display(Name = "I accept the terms and conditions")]
[MustBeTrue(ErrorMessage = "Please accept terms and conditions before continuing")]
public bool AcceptTerms { get; set; }

This solution was discovered on StackOverflow

Wednesday, 28 January 2009

ASP.NET MVC 1.0 Release Candidate Available

Microsoft have shipped the ASP.NET MVC 1.0 Release Candidate (RC). It's available to download from here. This is hopefully the last release before they ship the final release, which they are hoping to be next month.

The extensive release notes, all 26 pages detail the upgrade process from ASP.NET MVC Beta and includes all the details that have changed.