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

Requiring authentication for a whole ASP.Net MVC site

This is easy from ASP.NET MVC 4 onwards: http://blogs.msdn.com/b/rickandy/archive/2012/03/23/securing-your-asp-net-mvc-4-app-and-the-new-allowanonymous-attribute.aspx You can register a filter that will require users to be authenticated to access all controllers public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); filters.Add(new System.Web.Mvc.AuthorizeAttribute()); } but then they won’t be able to access to the Login controller, so you simply add the [AllowAnonymous] attribute to that controller: [AllowAnonymous] public ActionResult Login(string returnUrl) [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult Login(LoginModel model, string returnUrl)

February 28, 2014 · 1 min

Multiline Text Editor in MVC

In ASP.Net MVC, it’s so easy to create an entity data model and generate controllers and views for each entity. You can create lots of pages for editing your data just by clicking buttons… A downside is that the generated code doesn’t know the the difference between a small string and a large one, so you get the same small text input box to edit all strings. In your razor view, that’s created by ...

February 27, 2014 · 2 min

Intercepting FormView inserts and updates

Our FormViews in ASP.Net web forms tend to contain user controls in the templates. If you want to want to intercept the insert or update events, you end up with something like: protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e) { } But how do you access the values in the user control? We’ve often had code like this: TextBox tReviewerName = (TextBox)FormView1.Row.FindControl("dataReviewerName"); which is OK to get the value from that field if you want to code your own updates/inserts but you can’t (I think) write back to that field in time for the update to pass that new value back to the database. ...

January 27, 2014 · 1 min

AutoMapper

From http://stackoverflow.com/tags/automapper/info AutoMapper uses a fluent configuration API to define an object-object mapping strategy. AutoMapper uses a convention-based matching algorithm to match up source to destination values. Currently, AutoMapper is geared towards model projection scenarios to flatten complex object models to DTOs and other simple objects, whose design is better suited for serialization, communication, messaging, or simply an anti-corruption layer between the domain and application layer. Slightly slow but useful video to show how AutoMapper is useful ...

January 16, 2014 · 1 min