ASP.NET MVC Workshop Code

Thank you everyone who came to my one day ASP.NET MVC workshop at ASP.NET Connections. We managed to build an entire Movie Database application with unit tests and a reasonably good design — Congratulations!

I’ve attached the Movie Database application and the demo code from the workshop below.

Workshop Code

Discussion

  1. Jacob says:

    So, this just follows the same principles as the contact manager you posted on asp.net/mvc.

    I appreciate your work, however, I would like to request you use a different testing framework. MSTest is only available for VS Pro and Team. Something like nUnit would be nicer for us 2008 express edition users.

    Thanks.

  2. @Jacob – Yes, it has the same architecture as the Contact Manager application — when I find something that works, I stick with it 🙂

    Good point about NUnit for Visual Web Developers. Thanks.

  3. Muhammad Sharjeel says:

    hi Stephen,

    For 2 to 3 month, i am reading regarding ASP.net MVC Framework and also integrated it in my some intranet applications, it is awesome, it is solving alots of issues that i have faced in WebForm but i am also a big fain of WCSF, mainly it’s plug able architecture.

    We are designing a VOIP Exchange System that will work on Internet. For its web development, i had plan for WCSF but after designing some interanet application on ASP.NET MVC framework, i am thinking to desing my VOIP application using MVC but i need that plugable architecture.
    I were thinking to replace the MVP with MVC in WCSF but my team don’t have that much time.

    please advise me what should i do?

  4. Francis Robert says:

    I really like ASP.Net MVC for more than one reasons but there’s just one thing that keeps me from using it, I just cannot figure out how to do the following…

    All the tutorials I’ve found so far are based on a one-to-one model: list products, display one product, edit, create, delete… Same for your movie app.

    What I would like to do is to display a blog entry in a view, followed by the comments associated to it (different model), and then followed by a form to post a new comment, while using the same default validation codes (Html.ValidationMessage…) which are created automatically.

    I just can’t figure out how to achieve this as it always complains about the model not fitting the view (excuse my french…). So a tutorial replicating this example or some guidelines would be very much appreciated! 🙂

    Thanks for all your hard work!

  5. Eric says:

    @Francis: I’ve done that by creating a class that aggregates model objects and using that as the Model for the view. So if you needed to pass both a PostCollection and a CommentsCollection (or whatever), those would be properties on this third class…whatever you call it. Has worked great for me.

  6. @Francis, @Eric — Yes, you want to create a view model class that exposes both properties. Then, use the following Inherits attribute at the top of your view:

    Inherits=”System.Web.Mvc.ViewPage

    There is a sample of this in the workshop code if you take a look at the ViewsMovieIndex.aspx view.

  7. Francis Robert says:

    Thanks a lot!! I’ll go study that. Nice! 🙂

  8. Mokhtar Aiad says:

    I have viewed some examples but all of them have prerequities : using specific technologies(LinkToSQL .. and so on). Can you give ASP.NET MVC examples using well known and simple things like classic ADO databases manipulation. This will help many peoples.
    For example: How to construct an ASP.NET MVC application with this 3 things:

    1.A pure HTML/Javascript Form page with HTML input text tags for each field (say Customer.html of Notrhwind.mdb)
    The page have 2 buttons: NextRecord and PreviousRecord having at Onclick_Event:getRecord(NextOrPrevious)

    2.A javascript manual ajax GetRecord function, something like this:
    function getrecord(NextOrPrevious) {
    var oXMLHTTP = new ActiveXObject(“Microsoft.XMLHTTP”);
    var sURL = “getrecord.aspx?ID=” + NextOrPrevious;
    oXMLHTTP.open( “POST”, sURL, false );
    oXMLHTTP.send();

    var sResult=oXMLHTTP.responseText
    var aRecord = sResult.split(“;”);

    document.getElementById(‘CustomerID’).value = aRecord[0];
    document.getElementById(‘CompanyName’).value = aRecord[1];
    document.getElementById(‘ContactName’).value = aRecord[2];
    document.getElementById(‘Adress’).value = aRecord[3];
    … and so on …

    }

    3. What is the SHORTEST and the simpliest way to program getrecord.aspx ? or VB/C# getrecord controler function ? using something like ADODB Recordset. VB/C# GetRecord function will return a simple string containing the current record fields values separated by comma. (but without using other things like LinkToSQL, views, models , Json, XMl, or other entities.)

    Think you very much !

  9. josh says:

    Thanks a lot!

  10. EricB says:

    Your sessions were great Stephen. I really enjoyed them. I stayed the extra day for the MVC workshop and was glad that I did. I am NOT sold on the Entity Framework and I am disappointed with the inherent support for JavaScript in MVC but this framework seems to be right up my alley. Thanks again.

  11. Timur says:

    Are there any places where I can watch it? like MIX09 or PDC? Or this is just a “private” conference, in the sense that it’s not been published on the net?

  12. Alex says:

    Hi Stephen,

    Is it possible at your side to send me a step-by step guide on the Movie App so that I can practice at my free time? Thank you.

  13. Jack says:

    The code is of high quality, We can learn a lot from it!

  14. @Alex — There is both a written and video version of the Movie Application tutorial at http://www.ASP.net/mvc

  15. EricB says:

    I have a question about the design gallery. Why is that the master page references a code behind file? And where is it? It seems the layout blows in the face of MVC a bit or am I looking at it incorrectly. thank you. -E

  16. @Eric — Several of the designs in the Design Gallery are currently outdated because they were created while ASP.NET MVC was in BETA. We are planning on updating all of the designs to work with the final release of ASP.NET MVC within the next two weeks.

  17. designx9 says:

    Please let me know whether i could find the code in vb.net too.

    Thanks

  18. Eb says:

    What is the purpose of the Service Class – why are you adding an extra layer. Also, how would one change datastore? Say I am using MSSQL now and in the future decide to use XML instead, how and where would I do this? It was v easy in the Provider Model. Sorry but new to this Repository Pattern stuff.

  19. @Eb – According to the Single Reponsibility Principle (SRP) a class should have a single reason to change. You don’t want to mix validation logic with your controller logic and data access logic. In the sample application, the controller logic is in the controllers, the validation logic is in the service, and the data access logic is in the repository.

    Because classes that interact with the repository interact only with the IRepository interface, you can easily swap a new repository for the existing one. Just implement the IRepository interface with the new class.

    Hope this helps!

    Stephen

  20. Eb says:

    Thanks for your instant reply (you are good!!). OK get you bit about validation logic. I am still confused about plugging in various datastore. Only way I can think of changing repository is probably by loading Repository type via some configuration section in the Web.Config (like loading default provider in the Provider Model). Also, I think the type can be loaded via Global.asax.

    Please clarify – thanks

  21. @Eb — Yes, that’s right. If you don’t want to hard code the repository then you can (a) Use something like the provider model (b) Set the repository in the Global.asax (c) Use a Dependency Injection Framework like the Microsoft Managed Extensibility Framework(MEF).

  22. Alex L says:

    Stephen,

    Nice and simple video/tutorial.

    I followed the instructions to remove the id field from the Edit view by removing the following code:


    < %= Html.TextBox("CampaignId", Model.CampaignId) %>
    < %= Html.ValidationMessage("CampaignId", "*") %>

    and now I am receiving a NullReferenceException on the Model object in the next field of the Edit view:


    < %= Html.TextBox("CampaignName", Model.CampaignName) %>
    < %= Html.ValidationMessage("CampaignName", "*") %>

    What is causing the Model object to be null if the id is not present?

    Thanks!

  23. Alex L says:

    The HTML code was removed in the above. This is the line of code in the view where the Model object is null:

    Html.TextBox(“CampaignName”, Model.CampaignName)

    Is removing the id field from the Edit view causing the Entity Framework not to populate the Model?

  24. Karl Kopp says:

    Thanks – loving MVC but not a lot of code around at the moment, so this is great…

  25. Thanks for sharing. Nice to have source code available

  26. Very good example is given in attachment. I understand that very well. Thanks.

  27. wer Very good example is given in attachment. I understand that very well. Thanks.

  28. i like this post thanks!!!

  29. wer Very good example is given in attachment. I understand that very well. Thanks.

  30. This is the line of code in the view where the Model object is null.
    homeschool online

  31. I must say, very interesting and helpful article.

  32. online games says:

    Very good example is given in attachment. I understand that very well.

  33. Dating site says:

    MSTest is only available for VS Pro and Team. Something like nUnit would be nicer for us 2008 express edition users.

  34. NikeAir says:

    Nice article, very helpful. Thanks!
    ——————————–
    Nike Shoes || air yeezy

  35. Hello
    Stephen your posts are always useful. Thanks for sharing.

  36. scott says:

    Is removing the id field from the Edit view causing the Entity Framework not to populate the Model?
    Tiffany Jewellery | Ed Hardy | Tiffany Jewelry