Dropit redefines MVC: Presents V for classic ASP/VBScript

by scott.phrank 1. April 2010 11:27

Dropit has spent the last couple of months building a MVC framework for classic asp, or VBScript, and we finally feel that it's ready for release.

The biggest problem we have encountered during the development phase is the lack of objects in VBScript, hence we have discarded the M in MVC.

We have also decided to ignore the C since controllers and VBScript don't match.

But we have built a couple of Views for VBScript, please download them here.

Views.rar (42.06 kb)

Tags:

Development

Mvc ninjatips

by Christer 28. November 2009 22:41

I was lucky enough to get to check out Scott Hanselman's Pdc session "ASP.NET MVC 2: Ninjas Still on Fire Black Belt Tips" and got really impressed with a few of the features he mentioned.

First thing that hit me was DataAnnotations. I had seen it earlier but i wasnt that impressed as it looked like an easy way to do general validation on serverside, but i couldnt see any good use in the real world. But i changed my mind asap after this session =). If you havent seen DataAnnotations at work before i suggest you check out Johan Driessens excellent post about it at: Testing DataAnnotation-based validation in ASP.NET MVC

So, the "flaws" i noticed the first time i read about DataAnnotations where that it works nicely for code i write myself. But how about generated code? i dont want to modify generated code as my modifications will be removed when i regenerate it. Or if i dont have the code? it cant be used at all then. And lastly, if i dont want to mess up my nice clean code with a bunch of attributes used for validation..
The solution: Buddy classes. By using the attribute MetaDataTypeyou can specify another class that have all the metadata for your object. Since its in another class now it doesnt pollute your code anymore, and it maps them by name, so you can map them to stuff you know will exist in the future.
Also the annotations are extendable so you can write your own validators and they can be generated to clientscript if you want to use that (or write your own).

Another cool feature mentioned in that session was the DisplayForModel/EditorForModel-helpers that use templates to render your stuff.
An empty call to Html.DisplayForModel()/Html.EditorForModel() will use the modelname to look for a template. So it will try and load an ascx-file named <modelname>.ascx and use it to render \o/. You can also specify a name to load a named template.
And for properties there is an attribute you can use (UIHint) to specify a template for them aswell.

The coolest stuff of all is that since it tries to load a template for types you can use it on all existing types aswell! So by creating for ex a DateTime.ascx template you can specify how all rendering of a DateTime will be done.

As a sidenote it also supports fallback. (not mentioned in that session) so if you have an Employee that inherits Person, it will use Person.ascx if it cant find the Employee.ascx template. Or if you have a string it will fallback to object.ascx if it cant find your string.ascx! \o/

So all in all a nice bunch of simple tips that help keep your code clean and help you avoid repeating yourself, and im a big fan of both =).

Other stuff he mentioned was Customizing WebFormViewEngine to select templates depending on custom parameters. Using MvcTurbine to hook up IoC containers. That you can use <%: Model.Prop %> instead of <%= Html.Encode(Model.Prop) %> for automatic HtmlEncoding \o/. And some cool info on customizing the templates used to generate the default code (T4 templates).

If any of the above sounds interesting i suggest you take a look et the complete session available here http://microsoftpdc.com/Sessions/FT59Its about an hour and worth every minute spent watching it.

Tags: , , ,

Development

EPiServer Site Center, Gadgets, MVC, oh my…

by erik.nilsson 19. October 2009 19:38

I recently had some time over to play around with the next release of EPiServer, what the guys over there call CMS6, and the new feature EPiServer Site Center. The build I used was 5.3.223.42.

Getting it all up and running was easy enough with the public templates project. Now as for the Site Center (from here on known as SC) itself, it still needs some polishing since a lot of stuff don’t work yet, but it’s a early release anyways.

The first thing I did was to go through the “Creating a gadget” example (hello world, wohoo). And as I am new to MVC also, this was pretty much uncharted territory. Yes, site center uses MVC, hooray! I won’t go into detail on how Mvc works here, but let’s just say that it is stateless as opposed to webforms, so no more silly postbacks and messing around with viewstate. Oh, and also MVC gives us back our <form> tag, yay!

Writing gadgets is fairly straightforward, all you need to do is add [Gadget] as a attribute to your MVC Controller, register your assembly as a possible container of gadgets in web.config, and you’re ready to go. The first thing I noticed is that SC always use the “Index” action to render the gadget. And that it also always passes “gadgetId” as an argument. “gadgetId” is a guid generated by SC and is supposed to be the unique identifier for the gadget. I had some issues with saving/loading gadgets as it seemed to always generate a new Guid every time I recompiled, but it might not be implemented yet. Alternatively, I did it all wrong… Anyways, you can still write your fancy actions and play around with it.

Code, yay!

Anyways, let’s look at some code, the example EP provided with the release was so dull, since it didn’t actually do anything fun, so I decided to make a “quicknews” gadget, pay no attention to the silly things the “Save” action does, this was just to play around:

using System;
using System.Web.Mvc;
using EPiServer;
using EPiServer.Core;
using EPiServer.DataAccess;
using EPiServer.Shell.Gadgets;
using EPiServer.Shell.Gadgets.Contracts;

namespace Dropit.Gadgets.Controllers
{
    [Gadget( Name = "Quicknews" )]
    public class QuickNewsController : ControllerBase
    {
        public ActionResult Index( Guid gadgetId )
        {
            return View();
        }

        [AcceptVerbs( HttpVerbs.Post )]
        public ActionResult Save( FormCollection form )
        {
            PageReference parent = DataFactory.Instance.GetPage( PageReference.StartPage )
.Property.Get( "NewsRoot" ).Value as PageReference; PageData newsPage = DataFactory.Instance.GetDefaultPageData( parent,
"[Public] News item" ); newsPage.PageName = form.Get( "PageName" ); newsPage.Property[ "MainIntro" ].Value = form.Get( "Intro" ); newsPage.Property[ "MainBody" ].Value = form.Get( "MainBody" ); DataFactory.Instance.Save( newsPage, SaveAction.Publish ); return View( "Index" ); } [GadgetAction( ActionType.Menu, Text = "Settings" )] public ActionResult Settings( Guid gadgetId ) { return View(); } } }

The first thing I found when playing around with Reflector (the most important tool any EPiServer developer can have) in the SC dll’s, is the [GadgetAction] attribute, if you set the actiontype to Menu, you will get a fancy menu option in the gadget, which, if clicked, will automatically call that action (ideally a ActionResult with a view). I have done that in the example above to make a configuration menu, very nice. I would assume there will be more options coming there in future SC releases.

Onward to the views. Let’s take a look at some code:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ModelBase>" %>
<%@ Import Namespace="System.Web.Mvc.Html"%>
<%@ Import Namespace="Dropit.Gadgets.Models"%>
<%@ Import Namespace="EPiServer.Shell.Extensions" %>

<div class="defaultOffset">
    <% Html.BeginGadgetForm( "Save" ); %>
        
        Page name:<br />
        <%= Html.TextBox("PageName")%>
        
        <br /><br />
        Intro:<br />
        <%= Html.TextArea("Intro")%>
        
        <br /><br />
        Main body:<br />
        <%= Html.TextArea("MainBody")%>
        <br /><br />
        
        <input type="submit" value="Save" />
        <input type="button" value="Clear form" onclick="epi.gadget.loadView(this);" />
    <% Html.EndForm(); %>
</div>

Another fun feature of SC is that you cannot make forms and post them as you like, since your form action would take you directly to your defined action ( <form action=”/controller/action/params”>), which would of course be an action in your controller. Now your controller have no idea what the SC is, and will render directly to your browser, bummer. After reading a blog post by Roger Wirz at the EPiServer labs site, I noticed him using a Html helper method called Html.BeginGadgetForm(…). Html helpers are MVC functions that do exactly what they are called, they help you write html, and it is really easy to make your own, check the asp.net MVC site to see how they are created.

Anyways, the BeginGadgetForm helper puts another action on the form element, which help us post it correctly so the result will be rendered in the SC instead of directly to the browser. It might be done in more instances also, since the built in example does not seem to have this action if you view the files, possibly by using jQuery to parse all the forms or submit buttons, or hooking onto the renderer (someone who knows how it all work, feel free to enlighten me). Ah well, what really needs to be done is add, either to your form element, or as an action on your element that is used to submit the form. If you reflect the BeginGadgetForm, you see that it adds:

 

onsubmit ="epi.gadget.postAjaxForm(this); return false;"

 

Which will make it post the form via ajax instead of submitting the form normally (and taking you out of the SC as mentioned above). The input fields at the end of the form does exactly the same as if you would use the extension method GadgetOkCancelButtons. The onclick on the cancel button is used to clear the fields by simply reloading the current view and fetch any saved settings again.

Learning from this, I made my own very simple GadgetActionLink, which renders a form with the action set to your desired Controller action, and inside it a link with the href attribute set to “Javascript:epi.gadget.postAjaxForm(formName)” and a hidden field with a (optional) value. Now I can make fast links that do something without having to create all that stuff myself. Doing it with jQuery.ajax would probably be a lot better looking (instead of having lots of small forms cluttering your gadget), but this way was very easy to do on the other hand, since it utilizes the built-in posting method, which is a jQuery.ajax call anyways.

Conclusion

SC looks very promising, and the fact that it’s built with MVC makes it much cooler. I am looking forward to the next release, which will hopefully have some more features and documentation (especially regarding how we are supposed to save/load our gadgets, like a general guideline), and most of the quirks ironed out.
Creating gadgets is very simple, just make a Controller and a Partial View (usercontrol), and add the attribute [Gadget] to your controller., once you learn how to submit forms to SC, it gets even simpler.
If you are interested in learning more about gadgets,  I recommend to look at Roger’s post I mentioned earlier as he build a little more advanced gadget there than in the example supplied with the CTP.

Tags: , , ,

Development

ASP.NET MVC + SubSonic + T4 = Sweet!

by Johan Driessen 3. April 2009 08:34

I’ve been fiddling around with ASP.NET MVC and SubSonic 3 for a while (as I mentioned in a previous post), and also been learning some cool stuff about T4 Templating. Now Rob Conery has created a very cool Visual Studio template bringing all this together (be sure to watch the video). And it even includes single sign on capabilities with e.g. Google and OpenID! And yeah, lots of ajax with jQuery.

I tried using this yesterday, and apart from his template being a little bit broken it really works! I created a data driven MVC web application with my own custom database in like 30 minutes. And without any magic, unmaintainable code that you can’t fiddle with. It’s just plain MVC, with SubSonic domain objects.

So I started over with the Dropit Labs site (which is actually only a static html page right now), using this. And after 2 more hours work I’m now only a little styling away from replacing the static version with a new state-of-the-art ASP.NET MVC version that will look almost exactly the same! I’m hoping to find the time to use my crazy css skills this weekend, and being able to release it next week.

Tags: , , , , ,

Development

Coming soon: Dropit Labs

by Johan Driessen 2. December 2008 21:01

Some of you might have noticed that although this blog is located at labs.dropit.se/blogs, there is nothing at labs.dropit.se itself. Well, that is about to change, hopefully we'll get it up well before Christmas. The idea is that labs.dropit.se will be a place where we, the people at Dropit, can show off some of the cool stuff we do, and make it available to others.

I'm currently in the process of developing the first version of the labs site itself. The idea is something like codeplex, only scaled down about 3000 times. I'm well aware that there are several solutions to this already available and open sourced, but I'm using this as an excuse to explore ASP.NET MVC a bit more, by developing an actual application with it. It's really, really nice, and I wish I would get the opportunity to use it in customer projects as well. In fact, I'm trying to figure out some way to combine it with EPiServer...

For database access, I was going to use SubSonic 3.0 (which is only on Preview 2 - but hey, ASP.NET MVC is only in beta - the bleeding edge is more fun!), but it turns out the current version has a nasty bug that makes it crash if you try to pass a variable to the repository. A hard coded integer is fine, but not a variable. Go figure. :-)

So instead I'm using Linq to SQL, which is also pretty nice, although unfortunately doomed in the long run. As soon as Subversion 3.0 starts working again (Preview 3, perhaps?), I think I'll replace Linq to SQL again.

And of course, when we do launch labs.dropit.se, the labs site itself will be the first project available on labs! We still havent really decided which license to use for the stuff, but we'll try to make as much as possible open sourced, that's kind of the whole point anyway.

I'm also told that I have to put my old register-drinks-with-barcode-and-then-show-fun-charts application (that some of you might remember from a party last year - and some of you might not remember...) up there as well. But in that case I think I'll have to rewrite it, I'm not sure I really want to make my old ASP/VBScript-code available to the whole world... :-)

Tags: , , ,

Development

Creative Commons License
This work is licensed under a Creative Commons Attribution-Share Alike 2.5 Sweden License.


Welcome to the Dropit blog!

Here we, the people that work at Dropit, will write about stuff that interests us. For example web development, especially with .NET and EPiServer - but we'll also talk about other techniques that interest us, marketing on the web, social phenomenons, pop culture, games and software development in general.