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

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

NuGets of gold

“NuGet is a Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects that use the .NET Framework.” http://docs.nuget.org/ To be as agile as possible, the projects developed by the team tend to be coded as separate .Net projects, rather than within one over-arching framework. This makes it easy, when starting a project, to copy the elements from the most similar existing project and just get on with it, without worrying about breaking existing applications. The downside to this is that bugs that are identified and fixed in one project may also need to be fixed in many other projects. In the same way, design changes (updates to templates) need to be rolled out across many sites. Updates to libraries such as jQuery or .Net need to be rolled out across many sites too but there can be an advantage in that, as testing can be done in smaller chunks before the new version can go live – plus the rule of, if it ain’t broke, don’t fix it, can be applied to the older projects that don’t have any other reason to be updated. ...

January 16, 2014 · 4 min

Entity Framework – Model First

There are 3 ways to link entity framework to a database: Database first. Point Visual Studio at your database and it will create an entity model for you. Code first. Write your classes in Visual Studio and it can create your database for you. Model first. Use the Entity Data Model Tool in Visual Studio to model your entities. Visual Studio will can them create your database and classes for you. I’ve just tried Model First and not achieved what I wanted. ...

January 9, 2014 · 2 min