Getting started with MVC

by Mikael Henriksson 28. January 2009 23:19

There has been ALOT of improvement in the new version of MVC. I have just gotten started but so far I love it. It is expressive and feels much more intuitive that regular asp.net. You can pretty much extend the existing functionally as much as you like and only the sky is the limit. Wait, stars is the limit.. no there is no limit!

The new feature with sending a file straight to the browser is great. Basically you just create a “dummy” holder class (if you want) for file information like:

   1: public class Image
   2: {    
   3:     public int ImageId { get; set; }    
   4:     public string FileName { get; set; }    
   5:     public byte[] Content { get; set; }    
   6:     public string Mime { get; set; }
   7: }

Now you can easily choose how to send this to the browser. Question is what do you want to do with the file? You can force download or let the browser render it.

To download a file (image, pdf etc.) do the following

   1: public ActionResult DownloadImage(int id)
   2: {
   3:     ICriterion<Image> criterion = new Criterion<Image>(i => i.ID == id);
   4:     Repository repository = new Repository();
   5:     var image = repository.GetSingle(criterion);
   6:     return File(image.Content, image.MimeType, image.FileName);
   7: }

 

The only thing you have to do differently to render the image in the browser instead is to leave the file name out of the return statement. The third parameter just tells MVC to send the file as an attachment. The code to display the image:

   1: public ActionResult DisplayImage(int id)
   2: {
   3:     ICriterion<Image> criterion = new Criterion<Image>(i => i.ID == id);
   4:     Repository repository = new Repository();
   5:     var image = repository.GetSingle(criterion);
   6:     return File(image.Content, image.MimeType);
   7: }
 
The next thing I learned today and this is something I think everyone needs to learn is how to successfully render and display data from different views. Let’s take an invoice as example. The rows (invoice lines) belongs to the aggregate root Invoice and should not be accessible anywhere else. There for I wont create a view for invoice lines. But how do I display the invoice lines when I view the Invoice Details?
It’s real easy, kudos to everyone at the MVC team for making it so easy :) Create a UserControl like InvoiceLinesUserControl (Have a problem with long descriptive names? Read this blog post).

Since the release candidate of MVC the code behind of the view pages is gone. To tell the view what type of data is expected we now do that with Inherits:

   1: <%@ Page Title="" Language="C#" 
   2:     MasterPageFile="~/Views/Shared/Site.Master" 
   3:     Inherits="System.Web.Mvc.ViewPage<Core.Invoice>" %>

Then we need an action result to get some useful data to display in the view Invoice.Details.

   1: public ActionResult Details(long id)
   2: {
   3:     var invoiceRepository = new InvoiceRepository(companyId);
   4:     var invoice = invoiceRepository.GetSingle(id);
   5:     ViewData["Message"] = "Details for invoice : #" + invoice.InvoiceNumber;
   6:  
   7:     IList<InvoiceLines> invoiceLines = invoiceRepository.GetInvoiceLines(id);
   8:     ViewData["InvoiceLines"] = invoiceLines;
   9:     return View(invoice);
  10: }
From here on out it’s a walk in the park. To display the invoice details data I can just do 
   1: <%= Html.Encode(Model.InvoiceTotal) %>

and to render the user control with the quantity and unit price I just Render Partial and tell that method which view data to use like this:

   1: <% Html.RenderPartial("InvoiceLinesUserControl", ViewData["InvoiceLines"]); %>

Tags:

MVC

blog comments powered by Disqus

About the author

Life architect specialized in programming

Month List

Widget Twitter not found.

Root element is missing.X