Posts

Showing posts with the label MVC

Handling uploads with MVC4, JQuery, Plupload and CKEditor

I spent a considerable amount of time trying to get MVC to work nicely with various file uploaders while I was writing  RustyShark , and I have to admit it's been pretty frustrating at times. On the plus side, it's taught me a lot about how Microsoft MVC works behind the scenes, and allowed me to play with quite a few different uploading tools that're out there. My upload page on RustyShark has quite a complex model - each review is related to multiple Formats (PS3, 360, DVD, Blu Ray etc), each review / format combination can have multiple images associated with it, and each image has additional meta data attached, such as a caption and an upload date. The upload form is created using a combination of Editor Templates, JQuery and AJAX. I will be stripping down any examples to their bare bones to make things a little clearer. This article assumes you are familiar with MVC fundamentals such as creating controllers, views and models within Visual Studio 2010 or later, you ...

MVC3: Redirect Action to HTML Bookmark

I'm currently in the process of writing my next personal project,  RustyShark  in .Net MVC3. It's a game and films review site, and will hopefully generate a bit of a community. Each review on the website has a comments form at the bottom that readers can use to post their own mini review, or just a simple comment. I'm using unobtrusive client side validation, but (as it should) the validation also occurs on the server side. This means that the user could potentially receive an error message on the comments form after a post-back but not notice it, as they will be redirected to the top of the page, rather than the comments form. The "read review" page is accessed via the fairly standard route /Reviews/Read/{titleURL}, which means the reviews controller exposes the Read action method. The Read method has a single override which is tagged with the [HttpPost] attribute as follows: public class ReviewsController : Controller { public ActionResult R...

Visual Studio 2010 defaults to an undesired browser when opening MVC projects

A while ago, I set my default browser in Visual Studio 2010 to Google Chrome by using the "Browse With..." trick, and I've been happy with it for some time. Recently I've found that I've needed to do quite a bit of testing in IE, so I wanted to change it back. I went down the "Browse With..." route again, which seemed to work... but every time I close and re-opened Visual Studio, it reset itself back to Chrome. I've been living with it for a while, as I tend to do extended sessions anyway, so it's not been too much of a hassle... but today I've needed to close and restart Visual Studio a few times, so it's become a bit annoying. Anyway, to cut a long story short, it seems that when setting the "Browse With..." option in an MVC project, the settings aren't saved to the configuration file, probably because MVC projects don't actually have physical files to be "Browsed", unlike an ASP.Net project. I'm not ma...

MVC 3 Unobtrusive Validation with LINQ to SQL

Recently I've been working on a small project that uses LINQ to SQL as it's data layer (it's a very simple model) and contains two presentation projects - a web form application for the front end and an MVC 3 administration panel. When playing with the unobtrusive validation engine, you'll find many examples on the 'net telling you to decorate your model with various attributes from the  System.ComponentModel.DataAnnotations namespace. Now, I ran in to a bit of an issue with the LINQ to SQL objects - they're generated dynamically. I could add the attributes, but they would be overwritten any time I made a change to my DBML file. So, after a bit of searching I came across the MetaDataTypeAttribute in the same namespace. This attribute allows you to create a completely separate, standard class and define it as a Meta Data container for another class. Neat, hu? Well, I quickly ran in to my second issue - I could create my separate class, but I still had to...