ASP.Net MVC and URLs ending in a decimal or float

In your MVC site, you might want this URL to purchase 2.5 kilos of cheese for you https://www.mysite.com/cheese/purchase/2.5 Unfortunately it won’t be recognised as a route to the purchase method within the cheese controller. The good news is that this will work: https://www.mysite.com/cheese/purchase/2.5/ Note the extra slash at the end This is an explanation why: http://stackoverflow.com/questions/21387974/why-is-my-web-api-method-with-double-args-not-getting-called It’s fairly unusual to have a valid reason for a URL like this. If you want to pass a parameter that’s a float, it’s more likely that you would want to post the data.

November 14, 2014 · 1 min

What could go wrong with a simple jQuery value selector?

In this case the value selector is used like this: $(“.pPlacement select option\[value='” + PlacementId + “‘\]”).attr(‘selected’, true); When PlacementId changes from a number to text and that text then contains an apostrophe, then it breaks! To explain the code above, thee’s a dropdown list of placements and from somewhere we have the value of the one that we want to have as the selected option. The selector picks the option with the value equal to PlacementId and selects it. ...

November 7, 2014 · 1 min

Adding a stylesheet to an iframe

If your page contains an iframe and you need to dynamically add a stylesheet, you can do it like this: var $head = $(“#myIframe”).contents().find(“head”); $head.append($(“<link/>”, { rel: “stylesheet”, href: window.location.protocol + “//” + window.location.host + “/mystyles/mystylesheet.css”, type: “text/css” })); Even if the domain for the stylesheet is the same as the parent page and the iframe, you still need to include the host address of the stylesheet (the protocol and host bit) or IE will ignore it. from http://stackoverflow.com/questions/6960406/add-css-to-iframe and http://css-tricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/

September 22, 2014 · 1 min

EntityFramework Reverse POCO Generator and Views

EntityFramework’s own tool to generate a model from a database is slow and buggy. It’s particularly a problem if you update your database (change table names, field names etc.). EntityFramework Reverse POCO Generator is a great, lightweight replacement – it’s fast, configurable and just works. Also, the entities created can be the same as those created by the built in tool, so it is possible to switch to it part way through developing a project. ...

September 20, 2014 · 1 min

Casting one list to another with Omu.ValueInjecter

Omu.ValueInjecter is great for quickly casting one entity to another, especially when the property names match up, as there’s no configuration to do. This is really handy if you have a view model that’s just a cut down version of your model and you want to get the data from the model into the view model. If you’ve got a list of model entities that you want to cast to a list of view model entitites, you can do so without looping through them: ...

August 11, 2014 · 1 min

Steps in creating a new MVC application

Create the database Create the project/solution in Visual Studio don’t give the project the same name (in the same case) as one of the tables or the compiler will become confused between types and namespaces choose MVC and optionally Web API and unit tests leave authentication as Individual User Accounts Create an application in IIS that points to the project Change the Web properties for the project to use Local IIS ...

March 24, 2014 · 2 min

Changing the templates used to generate MVC views

If you use Visual Studio to generate MVC views, it will use T4 templates. This is how to edit those templates. Go to: C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Web\Mvc\Scaffolding\Templates\MvcView Make a backup copy of the folder (don’t put the backup inside the folder or VS will complain that it has multiple templates to choose from) Then, open all of the files (or at least all of the *.cs.t4) files) in a text editor. ...

March 21, 2014 · 1 min

Typical data annotations to use in an MVC data model

In ASP.Net MVC, the data model can use data annotations to describe how a property should be displayed or validated. [DisplayName("Label used for field goes here")] If a string field in the database (e.g. varchar()) doesn’t allow nulls you should either have [Required(ErrorMessage = "Required")] to provide a message when a field hasn’t been entered or [DisplayFormat(ConvertEmptyStringToNull = false)] if empty strings are allowed (though you might want to allow nulls if that’s the case) ...

March 20, 2014 · 1 min

Adding WYSIYYG editors to text fields in MVC

In a previous post I showed how you could set attributes on properties in an MVC model so that a text field would be edited with a multi-line HTML text box, instead of a single line input field. What if the you text you’re editing is HTML and you want to use an HTML editor? In the edit razor view, you could set some HTML attributes (e.g. CSS class) on the @Html.EditorFor() but you would have to do that on every view where you edit the field, and on every field that you want to use an HTML editor for. So this shows how to add an attribute to a property so that, wherever the property is edited, the HTML editor will be used. ...

March 18, 2014 · 2 min

Mobile emulation in Chrome

https://developers.google.com/chrome-developer-tools/docs/mobile-emulation This is great for testing responsive designs. It allows for device pixel ratios, so you’ll get a much better idea of what your site will look like than just by resizing your browser.

March 16, 2014 · 1 min