×

First time here?

You are looking at the most recent posts. You may also want to check out older archives. Please leave a comment, ask a question and consider subscribing to the latest posts via RSS or email. Thank you for visiting!

[previously named "Silverlight as the V in ASP.NET MVC" but changed per comments]

One thing that I’m excited about is learning new technologies.  Moving to the Silverlight team, I’ve moved away from a breadth of technology knowledge to something a bit more narrow.  Now I feel like all other developers trying to keep up with the technologies we are releasing.  As such, I’m a beginner for most.  One such technology is ASP.NET MVC, which was just released to release candidate stability.

I thought I’d play around with it in the context of Silverlight and use Silverlight as the “view” in the model-view-controller concept.  It’s easy to link the two.  In fact when you create a new Silverlight project, you now have the option of creating an ASP.NET MVC application as the host:

So right from the beginning, you can marry the two together.  Now from here, how can we leverage Silverlight as a view.  Well, here’s my take…learn with me (and comment where you’d do it differently/better and why).

First, I’m still going to create my MVC architecture.  I’m using the Northwind database for simplicity sake in this learning task.  I’ve created a LINQ to SQL data model to that database (which is a local SQLExpress instance).  I then wanted to take the simple task of showing the products by category and displaying them in a simple Silverlight application.

NOTE: Yes, this Silverlight view is basically just a layout of ListBoxes, but remember, this is just a learning experiment for us.  You may also ask yourself about Authentication/Authorization…again, this post is about an experiment and not a full-featured implementation, so there are bound to be missing pieces.

I decided to create a CategoryController and a ProductController which would handle the actions to retrieve list of categories and products and then drill into the detail of a product.  From there I still need some web View to be a container for my real view, the Silverlight application.  I created a View for Category, since essentially that’s the initial view my user would see.  All it is is an index page hosting my Silverlight application:

   1: <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="SilverMvc.Web.Views.Category.Index" %>
   2: <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
   3:     <p>
   4:         <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
   5:             <param name="source" value="ClientBin/SilverlightWithMvc.xap"/>
   6:             <param name="onerror" value="onSilverlightError" />
   7:             <param name="background" value="white" />
   8:             <param name="minRuntimeVersion" value="2.0.31005.0" />
   9:             <param name="autoUpgrade" value="true" />
  10:             <a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;">
  11:                  <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>
  12:             </a>
  13:         </object>
  14:     </p>
  15: </asp:Content>

Now all I have to do is start building my real view that will be here, the SilverlightWithMvc.xap application.  But first I obviously need my controllers to respond so some requests.  I wanted the CategoryController to display a list of categories and a list of products associated with a category.  But remember, my View will not be the ASPNET View, but rather my Silverlight app.  How then could I get the ViewData to be sent to my Silverlight application.

In MVC, typically your code will return a View from the action, similar to my Index view in my CategoryController:

   1: public ActionResult Index()
   2:         {
   3:             return View();
   4:         }

See where the return is an ActionResult (common) and the return is a View, also common.  This return would expect that there would be a View named Index in the project hierarchy, which in our case there is and again, is the initial hosting page for our app (located in /Views/Category/Index.aspx).  But what about our other data.  I wanted to add a controller action called List and Products that would list all the categories (List) and then list all the products for a given category (Products).  But I just wanted the data.  It turns out that the MVC framework can give us just the data instead of the View.  There is a Json return type.  So using my LINQ queries I created the two actions:

   1: public ActionResult List()
   2:         {
   3:             NorthwindDataContext db = new NorthwindDataContext();
   4:             var cats = from cat in db.Categories
   5:                        select new
   6:                        {
   7:                            cat.CategoryID,
   8:                            cat.CategoryName
   9:                        };
  10:  
  11:             return Json(cats);
  12:         }
  13:  
  14:         public ActionResult Products(int id)
  15:         {
  16:             NorthwindDataContext db = new NorthwindDataContext();
  17:             var prods = from prod in db.Products.Where(cat => cat.CategoryID == id)
  18:                         select new
  19:                         {
  20:                             prod.ProductID,
  21:                             prod.ProductName,
  22:                             prod.UnitPrice,
  23:                         };
  24:             return Json(prods);
  25:         }

As you can see the return type is Json and passing in the model data of what I need…as opposed to saying return View([modelData]).  I now get Json formatted data as a return result.  Great, now how to consume them?

Within Silverlight, we know we can consume Json data and we can take advantage of that capability to bring MVC and Silverlight together.  First let’s look at my layout UI code for the Silverlight application…it’s basically going to be a cascading ListBox view:

   1: <UserControl x:Class="SilverMvc.Page"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   4:     <Grid x:Name="LayoutRoot" Background="White">
   5:         <StackPanel Orientation="Horizontal">
   6:             <StackPanel Orientation="Vertical" Margin="0,0,25,0" x:Name="CategoryListing">
   7:                 <TextBlock Text="Select a category..." FontWeight="Bold" />
   8:                 <ListBox Width="200" Height="150" x:Name="CategoryList" ItemsSource="{Binding}" SelectionChanged="CategoryList_SelectionChanged" />
   9:             </StackPanel>
  10:             <StackPanel Orientation="Vertical" Margin="0,0,25,0" x:Name="ProductListings" Visibility="Collapsed">
  11:                 <TextBlock Text="Select a product..." FontWeight="Bold" />
  12:                 <ListBox Width="200" Height="150" x:Name="ProductList" SelectionChanged="ProductList_SelectionChanged" />
  13:             </StackPanel>
  14:             <StackPanel Orientation="Vertical" x:Name="ProductDetail" Visibility="Collapsed">
  15:                 <TextBlock Text="Product Details:" FontWeight="Bold" />
  16:                 <Grid>
  17:                     <Grid.RowDefinitions>
  18:                         <RowDefinition Height="Auto" />
  19:                         <RowDefinition Height="Auto" />
  20:                     </Grid.RowDefinitions>
  21:                     <Grid.ColumnDefinitions>
  22:                         <ColumnDefinition Width="Auto" />
  23:                         <ColumnDefinition Width="Auto" />
  24:                     </Grid.ColumnDefinitions>
  25:                     <TextBlock Grid.Column="0" Grid.Row="0" Text="Product Name: " />
  26:                     <TextBox Width="200" Grid.Column="1" Grid.Row="0" Text="{Binding Path=ProductName, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}" HorizontalAlignment="Left" VerticalAlignment="Top" />
  27:                     <TextBlock Grid.Column="0" Grid.Row="1" Text="Unit Price: " />
  28:                     <TextBox Width="200" Grid.Column="1" Grid.Row="1" Text="{Binding Path=UnitPrice, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}" HorizontalAlignment="Left" VerticalAlignment="Top" />
  29:                 </Grid>
  30:             </StackPanel>
  31:         </StackPanel>
  32:     </Grid>
  33: </UserControl>

Where one selection drives the next portion of the layout, etc.  You can see that I have several {Binding} statements in there as well as some selection changed handlers.  Let’s look at what happens on Loaded of the app:

   1: void Page_Loaded(object sender, RoutedEventArgs e)
   2:         {
   3:             WebClient mvc = new WebClient();
   4:             mvc.OpenReadCompleted += new OpenReadCompletedEventHandler(mvc_OpenReadCompleted);
   5:             mvc.OpenReadAsync(new Uri("http://localhost:33828/Category/List"));
   6:         }

You can see that I’m making a WebRequest call to a URI that happens to be our CategoryController with the List action command.  On the return event handler I’m getting that stream of data, which we know to be Json data, and setting the DataContext of my first ListBox:

   1: void mvc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
   2:         {
   3:             DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(List<Category>));
   4:             List<Category> cats = (List<Category>)json.ReadObject(e.Result);
   5:             CategoryList.DisplayMemberPath = "CategoryName";
   6:             CategoryListing.DataContext = cats;
   7:         }

Now you may at this point be asking a few questions about some of my decisions here within Silverlight.  First, the use of DataContractJsonSerializer.  Where is it?  Add a reference to System.ServiceModel.Web and you’ll get it.  You can also see that I’m using the ReadObject method and casting it to a List<Category>. 

Why not use System.Json and LINQ to JSON?  You could absolutely.  In doing so you could use your LINQ skills and get the data out of the Json stream and put it into a new object.  You’ll still have to create a local class representation because Silverlight can’t bind to an anonymous type.  This is the first reason I like just using the serializer.  The second reason is size.  I don’t know why I’m so picky, but I am.  Using the DataContractSerializer method here, my app is about 7K.  Adding a reference to System.Json and using those methods, my app is 27K.  For me, there is no additional benefit in code for what I’m doing to add that extra size, so I choose not to.  But you can…absolutely.  Your application needs may be different and the size cost/benefit analysis may result in a different outcome than mine…but there, I’ve stated my reasons here.

Where is Category defined?  Great question!  Category is a class defined in my Silverlight object that maps the data to a strongly-typed class..here’s the class definition:

   1: public class Category
   2:     {
   3:         public int CategoryID { get; set; }
   4:         public string CategoryName { get; set; }
   5:     }

Now that that is done, my data is retrieved, typed and bound to my UI.  Then in the category ListBox when a category is selected, we trigger a similar event to retrieve the products within that category and populate the product list using the same technique.  The final stage is that when a product is selected we populate a simple UI to show the details.  We could also have called another controller action, but here since we had most of the data in the product listing, we simply push the data to the DataContext of the layout container for the details:

   1: private void ProductList_SelectionChanged(object sender, SelectionChangedEventArgs e)
   2:         {
   3:             if (ProductList.SelectedIndex > -1)
   4:             {
   5:                 Product p = ProductList.SelectedItem as Product;
   6:                 ProductDetail.DataContext = p;
   7:                 ProductDetail.Visibility = Visibility.Visible;
   8:             }
   9:         }

You can see we get the SelectedItem as a Product (another internal class like Category) and make it the DataContext of the ProductDetail StackPanel who’s children have binding instructions.  The end result is this “view” below (the headings above the list boxes are not in the UI, but rather just labels to map to the controller action used to populate:

As I completed this little experiment, several things did come to mind…

  • What about deep linking?  The URI in the app didn’t change.
  • Now that I’m looking at a detail view, does the URI stating the initial view matter?

I think mostly these are “simple” things, but important to an MVC developer audience.  The interaction of URI semantics with Silverlight are important and things we are addressing in Silverlight 3 to make even this simple experiment easier.

So tell me, ASP.NET MVC experts, does this make sense?  Good/bad/what would you do different?  Help me learn how you would make the interaction between ASP.NET MVC and Silverlight better?

You can download my sample code for this project here (requires SQL 2008 and VS2008 with Silverlight and ASP.NET MVC tools installed): SilverlightWithMvc.zip


This work is licensed under a Creative Commons Attribution By license.


2/9/2009 5:06 PM | # re: Silverlight as the V in ASP.NET MVC
I'm no ASP.NET MVC expert, but I like your approach. One could argue that your SL code is not pure view code, as it calls your server, etc. even though it only does this on Page_Loaded. Could you pass in the data model as part of the embed code?

Alternatively, move the M and C over to the client as well. Obviously your model would make calls back to your web services, similar to how a server side model makes calls back to a database.
2/9/2009 5:15 PM | # re: Silverlight as the V in ASP.NET MVC
So how would you go about posting the data back to the Server? You could send back the data Form Encoded. I don't think that the asp.net mvc DefaultModelBinder doesn't support deserializing json.

I wonder if using mvc could be nice because you could build functioning prototypes in html, then without to much effort swap them out with Silverlight views.

Cheers
Jake
2/9/2009 6:12 PM | # re: Silverlight as the V in ASP.NET MVC
Hi!

Why good is it?
I preferred prism v2 for silerlight (mvp technology in client side, automatic modularity, etc)
I think better way to create a modularable silverlight architectura, and transfer data over wcf.

Prism v2: http://www.codeplex.com/CompositeWPF

Sorry for my english :)
2/9/2009 6:32 PM | # re: Silverlight as the V in ASP.NET MVC
I think you are breaking the whole MVC Pattern. You can probably have a similar thing in Silverlight which consist the Model/View/Controller, maybe your Model is calling the Server Side Service. But you cannot imply the Siverlight is serving as a View for ASP.NET MVC (as it acts more like a model/controllar than view)

If you post titled was Integrating Silverlight with ASP.NET MVC I would rather appreciate.
2/9/2009 7:31 PM | # re: Integrating Silverlight and ASP.NET MVC
Michael: good point. I suppose a "pure" view would just receive the data. This could be done with Silverlight still, but maybe my implementation here isn't the best example, because the resulting 'view' still has functionality.
2/9/2009 7:33 PM | # re: Integrating Silverlight and ASP.NET MVC
Kazi you have a great point here (and I renamed to be more accurate). How is it though that it breaks the *whole* pattern? Isn't the ASPX page itself just a View...in that sense that's what I'm trying to accomplish here...having a Silverlight app be a View. Perhaps in the current implementation of Silverlight this isn't the purist approach, but if the application was modular (i.e., if each of the lists was in fact their own control) it would be more of a View pattern.
2/9/2009 7:39 PM | # re: Silverlight as the V in ASP.NET MVC
Where can I learn more about ASP.NET MVC? Oh No No No ... Microsoft, again another new technology to learn? oh NO NO NO NO NO ...

I will complain to obama :-)
2/9/2009 7:46 PM | # re: Integrating Silverlight and ASP.NET MVC
Rick, ha! But if you're serious, I'd check out http://asp.net/mvc as well as http://www.stephenwalther.com for great resources.
2/9/2009 8:06 PM | # re: Integrating Silverlight and ASP.NET MVC
Good stuff Tim, I blogged about a similiar approach last summer:
http://blogger.forgottenskies.com/?p=125

My controller resulting an xmlresult and Silverlight taking those results and populating a XDocument (Linq to XML) type approach.

I also wondered the same thoughts - ie. 'isn't Silverlight just a RIA view where I want to write C# code and not deal with uncompiled jQuery ajax calls that are hard to track when the app gets complex'

But, I'm glad to see this piece. I just picked up John Papa's Silverlight book, and I think that I might push this all back a layer:

1. MVC - the controller calling the ADO.NET data services
2. Silverlight - acting more as a controller/view that calls the ADO.NET data services

But really, having the Silverlight as the view makes a ton of sense to me, because the controllers also allow me to create DTO objects that can be exchanged with services past the controller.

Just one more commment: Silverlight MUST fix this blurry font stuff - Silverlight looks great until you have to read the text. I'm leary of deploying it as I know my customers will start complaining about the blurriness of the fonts. (Especially black font on white background)
2/9/2009 8:23 PM | # re: Integrating Silverlight and ASP.NET MVC
Thanks, for changing the title. Now it makes complete sense.

The problem with Silverlight as View:

In MVC the View should not know from where it will get the data, the controller should be responsible for getting that from model for the view. Which is not possible with Silverlight. More specifically when you are calling the server side service in the Silvelight Page_Load it breaks the pattern as the view takes the responsibility to locate its data. The best option would be:

1. Silverlight View Loads, It initialized its Presenter.
2. The Presenter Calls ASP.NET MVC Controller.
3. The Presenter instructs Silverlight View to show some kind of Progress
3. ASP.NET MVC Controller prepares the data (It is the view in this case) from Model.
4. The Asp.net MVC Controller returns the View.
5. The Silverlight Presenter receives the data.
6. Silverlight Presenter instructs the silverlight view to update itself.
2/9/2009 8:53 PM | # re: Integrating Silverlight and ASP.NET MVC
Pardon my ignorance, but why is everyone arguing semantics here and trying to "fit" a Silverlight plug-in into the MVC model? I consider them seperate design architecture implementations.

The ASP.NET MVC application will be the "host" for the Silverlight application. The Silverlight application itself is more than likely designed the MVVP pattern (if you had the foresight to use ASP.NET MVC). Why try to fit one into another?

I consider the Silverlight XAP file completely modular and able to be hosted in a variety of places. I can host it in a ASP.NET web forms page. I can have my Silverlight XAP file hosted as a web part. I could have it hosted in Azure/Mesh. I can have it hosted in Silverlight 3 Mobile. Each time I change hosts, I should redesign my Silverligh module? I prefer to have that exact modularity and keeping the app to itself.

For example, if I want to host my Silverlight XAP in a Web part I will wrap it with: web part properties, personalization, connection information etc. These probably should be in their own seperate "wrapper class".

Isn't that what Silverlight is supposed to be all about? In my opinion, if you are writing for "one single host type" you might as well be writing Flash.
2/10/2009 5:29 AM | # re: Integrating Silverlight and ASP.NET MVC
I don't think that using SL like a view is a pretty good idea. Moreover, I'm still thinking, that it is not an actual ActionResult - it has to use additional web request instead of obtaining data directly from controller - there is no big sense in artificial widening of "MVC", the worst from 2 of worlds - thick-client-driven and thin-mvc-driven =(
2/10/2009 7:07 AM | # re: Integrating Silverlight and ASP.NET MVC
Prism v2! Prism v2! Prism v2! Prism v2! Prism v2! Prism v2! Prism v2! Prism v2!

Why do you want use serverside controller?

http://www.codeplex.com/CompositeWPF

2/10/2009 9:38 AM | # re: Integrating Silverlight and ASP.NET MVC
Kazi: I think I actually agree that perhaps if one was to follow a pattern the MVP or MVVM model would fit 'better' for Silverlight. But let me ask you this. ASPNET MVC includes a set of Ajax helper methods that communicate back to the server using javascript/async and are in the View. Has anyone ever argued then that the use of these in a View make these no longer Views?

meowth: see my comment to Kazi above. Are we entering a discussion of 'pure' MVC versus other interpretations perhaps? Similar to hi- and lo-REST definitions...but both are still REST?

Oh, and let's pre-empt interpet's reply: PRISM V2!!! :-)
2/10/2009 3:43 PM | # re: Integrating Silverlight and ASP.NET MVC
An MVC view can contain any type of view logic (or view widgets). Embedding Silverlight in an MVC view does not break the MVC pattern. Conceptually, it is no different than using Ajax in an HTML page to make asynchronous requests back to the server. In fact, you could switch out the Silverlight for an AJAX enabled HTML page in Tim's sample code and get something that everyone would agree is a perfectly fine example of an MVC web application.
2/10/2009 4:56 PM | # re: Integrating Silverlight and ASP.NET MVC
Hi Tim,

excellent post. It is exactly I need to my diploma paper.
2/10/2009 6:19 PM | # re: Integrating Silverlight and ASP.NET MVC
I liked the article, thank you Tim!
2/11/2009 12:02 PM | # re: Integrating Silverlight and ASP.NET MVC
I need a good article on Silverlight with Prism V2 :)
2/11/2009 12:07 PM | # re: Integrating Silverlight and ASP.NET MVC
oh, I should say on Prism V2:

It's all geared toward WPF and not Silverlight.

ie. 'hello world' app, etc...

2/15/2009 9:07 PM | # re: Integrating Silverlight and ASP.NET MVC
Thanks for this great post Tim, I've always wondered how MVC would go with Silverlight and databinding, and this is just as simple as using MVC in a classic pattern way!,

Cheers
Steve
silverlightcoder.net
2/17/2009 1:16 PM | # re: Integrating Silverlight and ASP.NET MVC
@Steve,

There is quite a lot of Silverlight in Prism V2 now (Drop 10) perhaps you had an earlier drop?

The team have done an excellent job of using a common code base for both a WPF and Silverlight library but some of the Silverlight documenattion needs to catch up.

Jason
2/24/2009 10:10 PM | # re: Integrating Silverlight and ASP.NET MVC
Thanks for the first take on what I am sure will be a popular design pattern. I am going to create an application for a client using some of these techniques. Maybe I can improve on what you have started.
3/5/2009 12:20 AM | # re: Integrating Silverlight and ASP.NET MVC
Hi Tim,

While integrating Silverlight into my MVC application I encountered a problem where the Silverlight dlls are not copied to the bin directory of my MVC application. Do I have to do something special to get these dlls copied?
3/5/2009 9:47 AM | # re: Integrating Silverlight and ASP.NET MVC
Gugulethu there are no Silverlight dlls that are on the server -- Silverlight is a client-side technology.
Gravatar
3/9/2009 9:37 AM | # re: Integrating Silverlight and ASP.NET MVC
Hi All,

I've taken a different approach and found a way of integrating Silverlight and ASP.NET MVC by passing model data in via Silverlight's InitParams.

I've blogged about it here if anyone's interested: blogs.msdn.com/.../asp-net-mvc-silverlight.aspx

Thanks,

(tim, keep up the great posts)
3/24/2009 5:26 PM | # re: Integrating Silverlight and ASP.NET MVC
Tim,

Thanks for the post. By the way can you give a small example of how you would post data from a Silverlight Client to a ASP.NET MVC backend. Tried using Silverlight WebClient using UploadSringAsynch and tried to get the posted data values on the ASP.NET MVC backend using Requst.Form["name"] ... without any luck. Moreover, it appears that my Request Object does not get populated with any data fro the "string" that is suppose to be posted from silverlight Webclient.

Regards,

BillC
3/25/2009 4:00 PM | # re: Integrating Silverlight and ASP.NET MVC
Arggg!

MVC = URIs are resources, not applications and asynchronicity is the name of the game.
Silverlight = applications on the client via the plugin model and a URI is just an entry point into the application where it's ability to perform synchronous activities on the client an incredibly powerful and interesting technology.
Web forms = applications over the server and a URI is just an entry point into the application. Synchronicity is encouraged through viewstate and session state. Like Silverlight, it's about making the stateless web seem stateful, but through server-side code.

Web forms and Silverlight go together, because they share the same purpose, which is to provide an experience over the web that is similar to the one that you get on the desktop.

MVC is about something else entirely. MVC is about statelessness and resources, not applications. Some Ajax makes sense, such as dynamically populating listboxes(as you've done in this example) or a treeview, but it should be used carefully, because once you start down that road, you end up making an MVC URI become an application, instead of what it's meant to be, which is a resource. In essence, you risk making MVC just Web Forms under another name.

We already have Web Forms and there's no reason to try to fit Web Form behavior into MVC just because we can. Don't try to fit a square peg in a round whole, just because you've got a sledgehammer and some duck tape lying around.
3/25/2009 4:32 PM | # re: Integrating Silverlight and ASP.NET MVC
John -- help me understand why providing a Silverlight UI is any different than providing an HTML UI to the same data?
3/25/2009 8:49 PM | # re: Integrating Silverlight and ASP.NET MVC
"John -- help me understand why providing a Silverlight UI is any different than providing an HTML UI to the same data?"

Suppose you were designing CBT training software and decided to use both MVC and Silverlight. You have dozens of pages of content. In addition, you ask the user 100 questions. Silverlight or Flash would seem to be a good fit, because users learn best when you can incorporate interesting visuals. The problem lies in the question about the state of a user's progress in the course. Silverlight, Flash and/or Ajax encourage you to keep state around. They encourage you to avoid the postback, because postbacks cause the screen to be re-drawn, which never looks good to a user.

MVC, on the other hand, does encourage you to do a postback for each question, because each question is a resource and naturally map to the question id. In MVC, you would expect to be able to navigate directly to a question by enterring a URI that looks something like MyCBT.com/questions/5.

With Flash, Silverlight and Ajax, you expect to go into the application and then it would be the responsibility of the application to provide you with a way to navigate to question 5. The two technologies go entirely in different directions. In MVC, you would either need to store the state of previously answered questions directly into the database after being answered or pass them to your DAL (e.g. NHibernate) to handle. In Silverlight, Flash, and Ajax it becomes easy just to keep adding state.

So what's the answer? In the case of the CBT course, use Web Forms, not MVC. Web forms were designed to make the stateless web appear stateful, which is what a CBT course needs. The same would be true for a game, where each move of the game adds meaningful state to the game.

MVC is a very popular topic right now and I admit that I'm personally excited about using it within my company, but I don't think that it makes sense with RIAs. RIAs are better served by Silverlight and Web Forms.

Silverlight is peanut butter and Web Forms are jelly. They make sense together. MVC is like bananas. Sure, Elvis like to make peanut butter and banana sandwiches, but for the rest of us, doesn't peanut butter and jelly sound a whole lot better?

But I'm open to discussion. Can you think of a compelling case where Silverlight adds significant value and works with MVC, but where Web Forms doesn't make sense?
3/25/2009 9:00 PM | # re: Integrating Silverlight and ASP.NET MVC
John -- all great points...especially the peanut butter and bananas :-). I think like any other offering, there is no one "right" solution (since "right" is in the eye of the architect/developer/application at the moment). The great thing is we have choices. Is Silverlight+ASPNET MVC the right one? Not for all cases no. Heck, Silverlight isn't the right choice for all cases as well :-). I think WebForms--really HTML--is more of a natural host for Silverlight sure...but I don't think that precludes anyone from benefiting from their MVC infrastructure and using Silverlight as the UI. Heck, I imagine some will use MVC framework for a quick REST API implementation only...I know MVC purists cringe when I'd say that...but I think it will be true.
3/26/2009 11:16 AM | # re: Integrating Silverlight and ASP.NET MVC
John -- agreed, MVP/MVVM are the preferred patterns for Silverlight/WPF development. These are the patterns exposed in the Prism 2 guidance as well as by many others.
4/25/2009 7:25 AM | # re: Integrating Silverlight and ASP.NET MVC
I think it's great to talk about the "Purist" or "Pure" implementation. The problem is, there is no such thing. People always start conversations with, "A purist would do this...", and then proceed to explain how they actually did it. These are simply guidelines to help you accomplish an end goal. As long as what you're doing accomplishes the end goal, who cares if it was a "Pure" MVC or "Pure" MVVM, etc.

I personally love the MVC framework, even though the pattern itself was deprecated a long time ago for the MVP, which was the split into two other patterns. The point is, Martin Fowler saw that these patterns were flawed. So basically, you're going to have to do what you have to do. And have fun doing it.
4/25/2009 3:33 PM | # re: Integrating Silverlight and ASP.NET MVC
Johnny, I'm with you...a pragmatic approach. Just make sure that whatever that approach is it is maintainable for the next person to come along, right ?! :-)
4/27/2009 5:15 AM | # re: Integrating Silverlight and ASP.NET MVC
Tim,

I took your idea and expanded it to have data sending and receiving with MVC + JSON:
jarrettatwork.blogspot.com/.../...es-with-mvc.html
4/30/2009 5:46 AM | # re: Integrating Silverlight and ASP.NET MVC
I have downloaded sample (SilverlightWithMvc.zip) and started it but SL control does not show on the MVC view. Help me correctly show Silverlight application on the ASP.NET MVC view (for ezample on the Views\Home\Index.aspx).
4/30/2009 7:39 AM | # re: Integrating Silverlight and ASP.NET MVC
VOBA -- in my sample I used a different view -- it isn't on the home page but a product page -- look there.
4/30/2009 12:06 PM | # re: Integrating Silverlight and ASP.NET MVC
OK, but it does not showa any Silverlight list boxes at the product page, why? I have installed SL 2 GDR 1 (2.0.40115.0) and ASP.NET MVC 1.0. Could some one help me to post any SL control on some MVC view? To show SL control on the .aspx page (ASP.NET Web Pages) I have used this:

<%@ Register Assembly="System.Web.Silverlight" namespace="System.Web.UI.SilverlightControls"
TagPrefix="asp" %>

<div>
<asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/SilverlightApp.xap"
MinimumVersion="2.0.30923.0" Width="100%" Height="831" Windowless="true"/>
</div>

and everything was OK!

Which script I have to use to post SL control in the ASP.NET MVC view?

4/30/2009 12:46 PM | # re: Integrating Silverlight and ASP.NET MVC
VOBA perhaps you don't have SQL Server running? This app uses a SQL database so you'll need to have it running for the data to appear.
5/6/2009 12:43 PM | # re: Integrating Silverlight and ASP.NET MVC
Hi !

Sad, I think, nr. X. Sad, because more and more samples I try, they will not work. Even this one. I am new to the stuff and see no change to find the reason out. I see no data. Just to be sure, I was correctly adapting the given connection string to my sql2005dev edition, I just downloaded and installed the express edition. No changes:No data.

A litte caution for newbies would really be good and save them a lot of time.

br--mabra
5/6/2009 1:53 PM | # re: Integrating Silverlight and ASP.NET MVC
mabra -- I could have been more clear, sorry for that. This does use SQL Server Express 2008 (also free). But all you need really is to attach the Northwind database if you don't want to use SQL2008 and make sure the model is updated to your specific connection string.
5/21/2009 12:37 AM | # re: Integrating Silverlight and ASP.NET MVC
Regarding the problem that silverlight category view with products was not shown and which was already reported in this thread , it is actually FireFox problem. I guess , it was tried in IE , but Chrome shows SL part properly as well Unfortunately my running numerous Silverlight demos for the last several months reminded the old joke about Java : write once, test everywhere. Other general comment related to the view state which also applies to the new navigation control in SL3 is that view state is not kept out of box. User should expect when she returns to "products" tab to see what was there when she navigated out. It would interesting to know how to keep this state and restore it properly and without too much work for various user controls. E.g. imagine Silverlight TreeView expanded deeply in many places and etc... Well in pure Silverlight I may stick to ControlTab container but for your scenario or SL3 navigation control should be unified approach to keep/restore a state.
5/25/2009 1:26 AM | # Web Design Company
While integrating Silverlight into my MVC application I encountered a problem where the Silverlight dlls are not copied to the bin directory of my MVC application. Do I have to do something special to get these dlls copied?
5/25/2009 7:52 AM | # re: Integrating Silverlight and ASP.NET MVC
Shahid -- the would get copied to a different directory depending on your project linking (ClientBin by default).
6/9/2009 11:52 AM | # re: Integrating Silverlight and ASP.NET MVC
Hi !

Sad:Just another not working silverlight example. This all - after month of studying and download samples:99% does even not work. Where to the hell is the development on this planet? In the kindergardden??????????

sorry--mabra
6/22/2009 9:11 AM | # re: Integrating Silverlight and ASP.NET MVC
Nice article. For what's said about Microsoft, as a coder I love to see this sort of stuff in action and love the stuff that's coming out of Redmond at the moment.
6/30/2009 11:23 AM | # re: Integrating Silverlight and ASP.NET MVC
First of all thanks to you Tim and all those other people who have contributed their nice ideas.

I tried your application. I did read through the reviews.

I also feel (like John) we may be overloading too many concepts in to a project just to be able to add it to our resume !!! ;). But on a different note I think if we use silverlight user controls for screens and are hosted with MVC it might be nice. I see that your usercontrol is linked to the test page rather than to the appropriate pages. I also see that applications might evolve and we might not actually use a round trip concept for all the pages but for only specific pages (a set of functionality). It also raises another interesting question on how many actions/discrete set of functionality do we have in our application to be able to justify the usage of MVC. I saw others response and samples. But it might need very high maintenance and skilled people to be able to build those sort of request /response and data contract oriented stuff.

Please add any architectural changes related to MVC and silverlight periodically.

Thanks once again
7/5/2009 12:38 PM | # Missing files in project
Hi !

Thanks for the description and your work. This looks like an interesting approach.

Bu please help me:The files "SilverlightWithMvcTestPage.aspx" and "SilverlightWithMvcTestPage.html" are missing from the project and it does not run. Beeing new to SL, it seems to impossible to repair that. Could you possibly please update this project?

Thanks a lot!

br--mabra

7/6/2009 12:29 PM | # re: Integrating Silverlight and ASP.NET MVC
mabra -- those files don't exist in this project as I'm using an MVC template and my content is in one of the Views.
7/7/2009 11:31 PM | # re: Integrating Silverlight and ASP.NET MVC
Hi !

Ok, thanks, I have now registered, that this is an error in the projectfile and I ignored this. But starting the right file and going to 'product's only gives me an silverlight error:

Error: Unhandled Error in Silverlight 2 Application An exception occurred during the operation, making the result invalid. Check InnerException for exception details. at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary()
at System.Net.OpenReadCompletedEventArgs.get_Result()
at SilverlightWithMvc.Page.mvc_OpenReadCompleted(Object sender, OpenReadCompletedEventArgs e)
at System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e)
at System.Net.WebClient.OpenReadOperationCompleted(Object arg)

Can you please give me a hint?? The SqlExpress is running well.

Thanks--mabra
7/13/2009 5:14 AM | # re: Integrating Silverlight and ASP.NET MVC
Hi!
When I try running your project in ie8, it's work fine.
But in FireFox 3.5 Silverlight object not visible.
Why?
Thanks
7/15/2009 8:03 PM | # re: Integrating Silverlight and ASP.NET MVC
I think it's a great solution. Why try and craft the perfect implementation when you can just go with what works? So what it breaks a few rules!! They were meant to be broken. At the end of the day you're using your favorite framework, you client is happy, and life goes on.
7/25/2009 3:20 PM | # re: Integrating Silverlight and ASP.NET MVC
Thank you for your great Post, I was looking for this combination MVC and Silverlight. Silverlight RIA just impressive and MVC is TDD oriented which I really fond of. Moreover combine them through JSON oh... very2 nice. a whole day trying to combine them and finally found the ready solution from your blog.
7/26/2009 1:21 PM | # re: Integrating Silverlight and ASP.NET MVC
I wonder how to integrate silverlight better, because MVC uses a lot of ajax and json. which pattern is better, whether to use javascript to manipulate silverlight as a control object (DLR approach) or the approach presented in this blog. at present I see the first approach seems to be more attractive because we can manipulate silverlight but also all DOM object, json comm is very easy with jquery or even there is also built-in ajax helper for it. is there any cons for it ?,
8/12/2009 5:24 PM | # re: Integrating Silverlight and ASP.NET MVC
Tim,
In your case you have only one silverlight page(page.xaml) which corresponds to index.aspx.
In your index.aspx, you call the xap as:
<param name="source" value="ClientBin/SilverlightWithMvc.xap" /> and the Application_Startup creates an instance of page.xaml.

What if you had more than one page( say page2.xaml, page3.xaml) which are called from within other views say index2.aspx, index3.aspx, respectively. Would you again have xap in the object tag of these aspx pages. If yes, this would call Application_Startup everytime user moves from index.aspx to index1.aspx, index2.aspx, etc and vice-versa. Also, how would Application_Startup know which xaml class to instantiate depending on the view of the MVC application which is calling it?
I am working on an application using a similar model and am at a loss as to how to proceed.
Regards,
Sandeep
8/17/2009 4:22 PM | # re: Integrating Silverlight and ASP.NET MVC
Hi Tim(or any other Guru),
Can anyone please reply to my question above?

Sandeep
8/18/2009 2:52 PM | # re: Integrating Silverlight and ASP.NET MVC
Sandeep - it seems like you are wanting to correlate a XAML page with an MVC page...I'm not sure why you want to do that.
8/22/2009 6:38 PM | # re: Integrating Silverlight and ASP.NET MVC
Nice post Tim. This one really got me thinking. I started replying, but it got long, so I made a post out of it myself. jeffdeville.com/.../...ul-principles-and-RIAs.aspx Would be interested to here what you think. In the meantime, I may have to go check out Prism... :-)
8/26/2009 8:03 AM | # re: Integrating Silverlight and ASP.NET MVC
Hi Tim

I have just started studying Silverlight.
I am looking for way how to bind Silverlight into MVC pattern as Views

Wow, here I find it.
Unforunately I have some problems with your sample because
I am using SQL Server 2005. Could you give me some advices?
My problem is as follows:
public ActionResult List()
...
return Json(cats);

OK, cats contains data,
but I cannot see the list objects on the webpage.
Has anyone got an idea?

Another question is:
Why JSON/LINQ? How about XML?
Maybe I will get the answer for the question above when the sample runs.

Thanks for your help
Chrs
CholonKid
8/27/2009 4:39 AM | # re: Integrating Silverlight and ASP.NET MVC
Hi Tim

now I found out.
The diff : u must use the IE 8.0
Within Firefox it won't show the result.

What should we do, so that it is not depend on browsers?

Chrs
Cholonkid
8/27/2009 8:32 AM | # re: Integrating Silverlight and ASP.NET MVC
CholonKid - I suspect it has something to do with how your container for the app is built (FF requires explicit sizing on certain elements)
8/29/2009 12:46 AM | # re: Integrating Silverlight and ASP.NET MVC
Hi Tim

thanks for your reply. I c the diff now.
BTW - I would like follow the opinion of Sandeep.
Could you only bind 1 xap ClientBin and 1 Page.xaml to one MVC apps?
It is not the correlation.
I think it is the context of each site.
i.e : I plan the build a webshop. then I will have many xaml and different functinal page.
Certainly I can build all in one Silverlight project and compile it to one xap file.
But I want to avoid a huge xap file.

What do you think? Have you got already some approach to this topic?

Chrs
Cholonkid
Gravatar
9/4/2009 12:28 PM | # re: Integrating Silverlight and ASP.NET MVC
Hi Tim, this might be related to what Sandeep was asking about.
In your example it looks like the code behind of the silverlight view calls the controller and then handles the conversion of the results.

But I am wondering as to where you would put logic for opening another view. Would that be in the same code behind of the main page? I am assuming we could have more than one page here...

Once the silverlight control is up, asp.net mvc framework is not longer taking care of the navigation among different views, from what I see.

Thank you.
9/10/2009 1:05 AM | # re: Integrating Silverlight and ASP.NET MVC
Hi Tim, I would also be interested in the solution of a real application scenario with more pages.

Regards!
9/19/2009 6:34 AM | # re: Integrating Silverlight and ASP.NET MVC
Hi, could you clarify the relations between RIA services and this approach on the client: aren't Domain Service extension a way to automate the creation of the client code for the data model (i.e. the category class in the example complete with CRUD methods?) if so can we expect to use this RIA services once the pass the preview stage?
10/22/2009 10:24 PM | # re: Integrating Silverlight and ASP.NET MVC
Steven -- is your fundamental struggle getting the data out of MySQL? Do you have a web service that exposes the data? That's all you need for Silverlight.
10/23/2009 3:38 AM | # re: Integrating Silverlight and ASP.NET MVC
Ohhh so I'm on the good road because about 10 minutes later I was reading something similar saying a web service has to be created and I found how to. Thx to point me right ^^

PS: yes, it was what makes me struggling ;P
10/26/2009 12:09 PM | # re: Integrating Silverlight and ASP.NET MVC
Hi Tim, Steven here again! We have been working alot on getting this fitting. Sounds like the webservice ain't easy thing to handle with all the messed up tutorials using SL 2 beta 2... It makes us searching for hours for stuffs that don't even exist anymore apparently(from what few members of the team said). So now I'm wondering if you'd be up to make a guide on how using MySQL database along with silverlight to write AND read the database. So far we figured out that a PHP script can read our database and put the datas in XML file. Then with LINQ to XML we could try to read this and use it in silverlight but it gets confusing for everyone (looked for hours on the web). I guess it would be same way to write in the database... silverlight -> LINQ to XML -> php -> database. The whole team would be glad if you can clarify things for us :D


Thx

Steven~
11/11/2009 5:51 AM | # re: Integrating Silverlight and ASP.NET MVC
Hey Tim,

Thanks for this great post! Is it okay if I make a request or suggestion on how to make it better? Can you please show an update on how to do this with 1-many and/or many-many relationships? THIS is where I've seen MVC start to have problems because of the attach state. Part of the problem with a lot of the examples online right now show one aspect really well (for example: "Silverlight and MVC", or "Entity Framework and MVC", or "Table-per-concrete-type and MVC"), but don't really explain what happens when you put the pieces together. For example:

I'm writing a request portal using:
- MVC 1.0 as the Framework
- Entity Framework for the data model (with 1-many relationships and table-per-concrete-type inheritance)
- SQL 2005 as the database
- Windows authentication (intranet only app)
- I'm still trying to modify it so that the Silverlight 3 is swapped in as the "View" instead of ASPX pages.

I almost have it. I had the other pieces of it working with ASPX, but switching the "View" to Silverlight 3 is harder than I thought because of the context. I don't want to give up and settle for using AJAX instead because I really want to use the Silverlight 3 web tools (like the Calendar) in my user control panels, plus it just flows nicer for the user experience.
Gravatar
12/13/2009 2:28 PM | # re: Integrating Silverlight and ASP.NET MVC
in Page_Loaded the Uri is hard coded.
mvc.OpenReadAsync(new Uri("http://localhost:33828/Category/List"));

Everything works great in VS with cassini but I cannot figure out how I can deploy this to my hosting environment.

Any ideas would help greatly!!!
12/13/2009 4:12 PM | # re: Integrating Silverlight and ASP.NET MVC
Jim -- you would put a URI that reflects the controller you are calling -- or build up a relative one.
1/16/2010 8:18 AM | # re: Integrating Silverlight and ASP.NET MVC
this is very informative
1/18/2010 9:55 AM | # re: Integrating Silverlight and ASP.NET MVC
Excellent post a unique source to learn about silver light development please keep it up Tim Heuer.
1/19/2010 7:37 AM | # nice
Thank you for posting such a useful website. Your weblog happens to be not just informative but also very stimulating too. There are a limited number of people who are capable of write technical articles that creatively. we are on the lookout for information regarding this topic. We ourselves went through several websites to find knowledge with regard to this.I will keep coming back !!
1/29/2010 8:00 AM | # re: Integrating Silverlight and ASP.NET MVC
A twin mattress is small enough to fit in just about any size room without taking over the entire room. Spare bedrooms usually aren’t large enough to accommodate large beds in addition to furniture
2/22/2010 11:53 AM | # re: Integrating Silverlight and ASP.NET MVC
If you want to migrate this solution to VS 2010 beta 2 (or higher), make sure that you change lines 28 and 41 of CategoryController.cs, otherwise you will get an exception. Line 28 should read:
return Json(cats, JsonRequestBehavior.AllowGet);
(note the added second argument). Same for line 41.
4/2/2010 6:08 AM | # re: Integrating Silverlight and ASP.NET MVC
Tim, I am getting and error when the function _client_OpenReadcompleted is called. The e parameter is returning the following error:

- e.Result 'e.Result' threw an exception of type 'System.Reflection.TargetInvocationException' System.IO.Stream {System.Reflection.TargetInvocationException}


This the code:

private void b1_Click(object sender, RoutedEventArgs e)
{
WebClient _client = new WebClient();

_client.OpenReadCompleted += new OpenReadCompletedEventHandler(_client_OpenReadCompleted);
_client.OpenReadAsync(new Uri("http://localhost:56397/home/list"));

}

void _client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(List<clsClient>));
List<clsClient> res = (List<clsClient>)(json.ReadObject(e.Result));
MyGrid.ItemsSource = res;

}

4/2/2010 8:50 AM | # re: Integrating Silverlight and ASP.NET MVC
make sure the *web* project is the startup project.
4/8/2010 1:38 PM | # re: Integrating Silverlight and ASP.NET MVC
I agree with your argument that what you are doing is very similar to Ajax in webform views. On that note I disagree with "View should just accept data" as views do have links that specify which controller to call and which action to be performed.

And I assume silverlight Navigation framework would be the answer to load new views.

Correct me please.
Thanks.
Gravatar
4/20/2010 10:19 AM | # re: Integrating Silverlight and ASP.NET MVC
I am interested from another point of view and would be curious if you think this makes sense. I am looking at a fairly complex asp.net site, and would like to enable it for many different client technologies, including various mobile platforms. Does it make sense to convert it to MVC and use it to allow my views to be flexible, I could allow silverlight for the View, like this example, if available to the user. It is a consumer site and I want the ultimate in flexiblity to deliver the richest client experience for the given client browser but without having to recreate the business logic.
5/26/2010 8:21 PM | # re: Integrating Silverlight and ASP.NET MVC
I had been thinking the same thing, and therefore began doing some work in AJAX again. What I realized is that after using silverlight,especially with Expression Blend, if I never see another CSS, even with jQuery, it will be too soon. The things that I can do in silverlight with Visual Studio and Blend can't be matched by the most skilled CSS type. Here, of course, I am assuming that stylesheets and some java script library will be essential elements with HTML 5. Just can't go back.
8/2/2010 12:32 AM | # re: Integrating Silverlight and ASP.NET MVC
This blog is so nice to me. I will keep on coming here again and again. Visit my link as well
watches
8/12/2010 7:28 AM | # re: Integrating Silverlight and ASP.NET MVC
Great article. The problem I am having is, I have a class file in my silverlight project and one in my mvc project. The class file in my silverlight project can not be annotated with [DataContract] and [DataMember] because it will error when I try to return JSON objects from MVC Controller to silverlight. The class file in my MVC project HAS to be annotated with [DataContract] and [DataMember] and in addition, I have to implement IModelBinder because the JSON object will never reach the controller (in scenarios where I need to save an object from silverlight back to controller). Putting the class file into a Silverlight Class Library and referencing the class file from both Silverlight and MVC app doesn't work due to the reasons I just listed. So I am stuck maintaining two class files... any ideas on how I can fix this?
8/12/2010 7:48 AM | # re: Integrating Silverlight and ASP.NET MVC
By the way, putting the class file into a Silverlight Class Library and referencing the class file from both Silverlight and MVC app doesn't work because I can't reference the namespace System.web.mvc needed for the ModelBinder or ActionFilterAttribute.
10/22/2010 1:03 PM | # re: Integrating Silverlight and ASP.NET MVC
I found this eXcelent article while Bingeling for information about using expression blend 3 with SilverLight 4 with asp.net 4 and MVC 2 (or MVC 3 'cause I really want to use the razor views, but I digress.)
11/23/2010 8:17 PM | # re: Integrating Silverlight and ASP.NET MVC

Great Vision
I absolutely agree with you and thank you for pointing out several relevant and important examples. Several blog contributors have written extensively on this topic. Thanks for these sharing.
replica watches |
rolex replica |
handmade jewelry |
lockets.
11/23/2010 8:29 PM | # re: Integrating Silverlight and ASP.NET MVC


Great Vision
I absolutely agree with you and thank you for pointing out several relevant and important examples. Several blog contributors have written extensively on this topic. Thanks for these sharing.
replica watches |
rolex replica |
handmade jewelry |
lockets.
1/4/2011 8:04 AM | # re: Integrating Silverlight and ASP.NET MVC
Great post! It works fine.
But one question. If I copy <object data="data:application/x-silverlight-2," type="application/x-silverlight-2"...</object> silverlight into About.aspx view it doesn't work. I can see "Silverlight" context menu but no silvelight content is loaded. Does anyone have the same problem?
1/13/2011 6:14 AM | # re: Integrating Silverlight and ASP.NET MVC
i am migrating silverlight 3.0 ,MVC 1.0 TO silverlight 4.0 and Windows Azure MVC
2.0 respectively. i am getting the error in e.Result when jason read the the data

and the error is

System.Reflection.TargetInvocationException was unhandled by user code
Message=An exception occurred during the operation, making the result invalid. Check InnerException for exception details.
StackTrace:
at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary()
at System.Net.OpenReadCompletedEventArgs.get_Result()
at HomeViewGraphs.Graphs.OpenReadCompleted(Object sender, OpenReadCompletedEventArgs e)
at System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e)
at System.Net.WebClient.OpenReadOperationCompleted(Object arg)
InnerException: System.Net.WebException
Message=The remote server returned an error: NotFound.
StackTrace:
at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)
at System.Net.WebClient.OpenReadAsyncCallback(IAsyncResult result)
InnerException: System.Net.WebException
Message=The remote server returned an error: NotFound.
StackTrace:
at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)
at System.Net.Browser.AsyncHelper.<>c__DisplayClass2.<BeginOnUI>b__0(Object sendState)
InnerException:


and the code

private void PageLoaded(object sender, RoutedEventArgs e)
{
WebClient mvc = new WebClient();
mvc.OpenReadCompleted += OpenReadCompleted;
string hostString = Host + "/GraphData";
mvc.OpenReadAsync(new Uri(hostString));
}


private void OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(GraphData));
GraphData model = (GraphData)json.ReadObject(e.Result);

var activeMembers = model.ActiveMembers;
SourceChart.Title = "Top 3 Idea contributors in current month";
SourceChart.Series.Add(new BarSeries
{
DataContext = activeMembers,
ItemsSource = activeMembers,
DependentValuePath = "IdeaCount",
IndependentValuePath = "Quarter",
DataPointStyle = this.Resources["ToolTipStyle"] as Style
});

}

Please help me for getting this error out.
thanks is advance.
1/27/2011 11:56 AM | # re: Integrating Silverlight and ASP.NET MVC
In fact when you create a new Silverlight project, you now have the option of creating an ASP.NET MVC application as the host las vegas web designers
Gravatar
1/29/2011 9:59 AM | # re: Integrating Silverlight and ASP.NET MVC
Maxi View was created from a passion and obsession to develop the World-Best
Blind Spot and Lane Change Mirror. Inspired by the annoyance and inadequacy of using several other spot mirrors, bubbles and fish eyes of all shapes and sizes, in addition to the factory ride side mirrors that say "Things are closer then they appear". Maxi View sought to find the World-Best Blind Spot and Lane Change Mirror. After searching the United States and many other countries in the mid 1980's, and taking 10 years to acquire all the patents, we are now able to bring you Maxi View, which we believe is the World-Best Blind Spot and Lane Change Mirror invention. If you share the same passion for finding the World-Best Blind Spot and Lane Change Mirror System, or you just want the most safety at the best price, remember, we only make one product and it's Maxi View. It has to be the best!
1/31/2011 11:50 AM | # re: Integrating Silverlight and ASP.NET MVC
I like the article. There are cases that this comes in handy. I have a site for a client that is using the MVC model. Works great, he wants to add a chat feature and I decided silverlight to do the chat. Going to the cotroller directly or a service would be the question. I did the controller to grab some info like a list of users in jason format and works great. So I decided on this format. Now trying to figure out how to write data to the control like when a user sends a message.
2/7/2011 4:48 AM | # re: Integrating Silverlight and ASP.NET MVC
this will only work for the "Index" view of a Controller. Try it against any other view name besides "Index" , and it will fail to hook up the events of the Silverlight object. which is a pity but i still like the idea of this mash-up.
2/19/2011 10:39 AM | # re: Integrating Silverlight and ASP.NET MVC
You obviously need to define the .thumbnails class and an id of largeimage... but you need to explain how to implement that. Outdoor Swing
2/27/2011 6:44 PM | # re: Integrating Silverlight and ASP.NET MVC
2/27/2011 7:21 PM | # re: Integrating Silverlight and ASP.NET MVC
Great information a visitve designed turn around the formula mentioned on this post. My team is currently trying to implement the sa b visit
3/1/2011 9:06 AM | # re: Integrating Silverlight and ASP.NET MVC
Thanks a lot for the sharing.

dänisch übersetzer | japanisch übersetzer
4/2/2011 12:20 AM | # re: Integrating Silverlight and ASP.NET MVC
thing that I’m excited about is learning new technologies. Moving to the Silverlight team, I’ve moved away from a breadth of technology a visit b visit c visit d visit
4/2/2011 12:25 AM | # re: Integrating Silverlight and ASP.NET MVC
I’m excited about is learning new technologies. Moving to the Silverlight team, I’ve moved away from a breadth of technology knowledge e visit f visit g visit h visit
4/2/2011 12:30 AM | # re: Integrating Silverlight and ASP.NET MVC
I’m a beginner for most. One such technology is ASP.NET MVC, which was just released to release candidate stability i visit j visit k visit l visit
4/2/2011 1:28 AM | # re: Integrating Silverlight and ASP.NET MVC
One such technology is ASP.NET MVC, which was just released to release candidate stability m visit n visit o visit p visit
4/2/2011 1:37 AM | # re: Integrating Silverlight and ASP.NET MVC
learning new technologies. Moving to the Silverlight team, I’ve moved away from a breadth of technology knowledge to something u visit v visit w visit x visit
4/2/2011 1:41 AM | # re: Integrating Silverlight and ASP.NET MVC
Now from here, how can we leverage Silverlight as a view. Well, here’s my take…learn with me (and comment where you’d do y visit z visit aa visit ab visit
4/2/2011 1:45 AM | # re: Integrating Silverlight and ASP.NET MVC
which would handle the actions to retrieve list of categories and products and then drill into the detail of a product. From there I still ac visit ad visit ae visit af visit
4/2/2011 1:48 AM | # re: Integrating Silverlight and ASP.NET MVC
ProductController which would handle the actions to retrieve list of categories and products and then drill into the detail of a product ag visit ah visit ai visit aj visit
4/2/2011 1:59 AM | # re: Integrating Silverlight and ASP.NET MVC
this make sense? Good/bad/what would you do different? Help me learn how you would make the interaction between ASP.NET ak visit al visit am visit an visit
4/2/2011 2:04 AM | # re: Integrating Silverlight and ASP.NET MVC
the two together. Now from here, how can we leverage Silverlight as a view. Well, here’s my take…learn with me (and comment ao visit ap visit aq visit ar visit
4/2/2011 2:16 AM | # re: Integrating Silverlight and ASP.NET MVC
thing that I’m excited about is learning new technologies. Moving to the Silverlight team, I’ve moved away from a breadth of technology aw visit ax visit ay visit az visit
4/2/2011 2:21 AM | # re: Integrating Silverlight and ASP.NET MVC
does this make sense? Good/bad/what would you do different? Help me learn how you would make the interaction between ASP.NET ba visit bb visit bc visit bd visit
4/2/2011 2:24 AM | # re: Integrating Silverlight and ASP.NET MVC
in the model-view-controller concept. It’s easy to link the two. In fact when you create a new Silverlight project, you now have the be visit bf visit bg visit bh visit
4/2/2011 2:28 AM | # re: Integrating Silverlight and ASP.NET MVC
context of Silverlight and use Silverlight as the “view” in the model-view-controller concept. It’s easy to link the two. In fact when you bi visit bk visit bl visit bm visit
4/2/2011 2:33 AM | # re: Integrating Silverlight and ASP.NET MVC
learning new technologies. Moving to the Silverlight team, I’ve moved away from a breadth of technology knowledge to something a bit bn visit bo visit bp visit bq visit
4/2/2011 2:39 AM | # re: Integrating Silverlight and ASP.NET MVC
something a bit more narrow. Now I feel like all other developers trying to keep up with the technologies we are releasing. As such, I’m br visit bs visit bt visit bu visit
4/2/2011 4:04 AM | # re: Integrating Silverlight and ASP.NET MVC
away from a breadth of technology knowledge to something a bit more narrow. Now I feel like all other developers trying to keep bv visit bw visit bx visit by visit
4/2/2011 4:15 AM | # re: Integrating Silverlight and ASP.NET MVC
and use Silverlight as the “view” in the model-view-controller concept. It’s easy to link the two. In fact when you create a new Silverlight project bz visit ca visit cb visit cc visit
4/3/2011 2:36 AM | # re: Integrating Silverlight and ASP.NET MVC
As such, I’m a beginner for most. One such technology is ASP.NET MVC, which was just released to release candidate stability cd visit ce visit cf visit cg visit
4/3/2011 2:41 AM | # re: Integrating Silverlight and ASP.NET MVC
I’ve moved away from a breadth of technology knowledge to something a bit more narrow. Now I feel like all other developers trying cd visit
Gravatar
4/10/2011 10:39 PM | # re: Integrating Silverlight and ASP.NET MVC
Moving to the Silverlight team, I’ve moved away from a breadth of technology knowledge to something a bit more narrow ch visit ci visit cj visit
4/10/2011 10:45 PM | # re: Integrating Silverlight and ASP.NET MVC
I feel like all other developers trying to keep up with the technologies we are releasing. As such, I’m a beginner for most. One such technology ck visit cl visit cm visit cn visit
4/10/2011 10:49 PM | # re: Integrating Silverlight and ASP.NET MVC
Silverlight and use Silverlight as the “view” in the model-view-controller concept. It’s easy to link the two. In fact when you create co visit cp visit cq visit cs visit
4/10/2011 10:58 PM | # re: Integrating Silverlight and ASP.NET MVC
other developers trying to keep up with the technologies we are releasing. As such, I’m a beginner for most. One such technology ct visit cu visit cv visit
4/21/2011 4:56 AM | # re: Integrating Silverlight and ASP.NET MVC

This post is pretty informative. I really never thought I could have a good read until I found your site using Google search engine. Thanks for taking the time to write down this article and being generous enough to share your knowledge with us. Bali Homes
Bali Villas
5/5/2011 1:53 AM | # re: Integrating Silverlight and ASP.NET MVC
This is a great blog posting and very useful. I really appreciate the research you put into it. Thanks software development company in india
5/6/2011 2:46 AM | # re: Integrating Silverlight and ASP.NET MVC
finally i find something that i want to know..
thanks for this usefull informations..
10/5/2011 10:51 PM | # re: Integrating Silverlight and ASP.NET MVC
Hi Tim,
I started the SilverlightWithMvc program with Visual Studio 2010, in the line:

"List<Category> cats = (List<Category>)json.ReadObject(e.Result);"

gives me an exception, but I feel that there are days I can not find the problem . Do you have any suggestions?

Thanks
10/6/2011 8:57 AM | # re: Integrating Silverlight and ASP.NET MVC
Hi Tim,

I made ​​some progress, I noticed that you create an exception in the second parameter "OpenReadCompletedEventArgs e" passed to the function "mvc_OpenReadCompleted".

There is some problem in data recovery, but I can not find it.

Do you have suggestions?

Thanks again.
Gravatar
10/26/2011 4:31 PM | # re: Integrating Silverlight and ASP.NET MVC
Hey Tim,

I'm experiencing the same error as both Mabra and <lucs at this time. My download of the sample code errors out on the following code line:

Page.xaml.cs(Line 37)

List<Category> cats = (List<Category>)json.ReadObject(e.Result);

Note: I migrated the sample code to VS2010\SL4 and connect to a standalone SQL database, not the embedded one.

Any ideas?

MG
4/25/2012 8:47 PM | # re: Integrating Silverlight and ASP.NET MVC
Hey~ Tim, it's a good post. Obviously silverlight is more powerful than the view of ASP.NET MVC. In my company, some of projects integrate Silverlight into view too. Because it's easier to complete some UI functionalities.
6/11/2012 6:43 PM | # re: Integrating Silverlight and ASP.NET MVC
Good post. Its a new information regarding Silver light.
2/26/2013 4:48 AM | # re: Integrating Silverlight and ASP.NET MVC
how to get the rtl lite application work in windows media center (win7) thanks for all the help in advance
8/4/2013 12:40 PM | # re: Integrating Silverlight and ASP.NET MVC
Hello Tum,
Thank you for your lovely post. But i have a problem While integrating Silverlight into my MVC app I encountered then the Silverlight dlls are not copied to the bin directory. I need your help Can you help me ?

 
Please add 2 and 3 and type the answer here:

DISCLAIMER:

The opinions/content expressed on this blog are provided "ASIS" with no warranties and are my own personal opinions/content (unless otherwise noted) and do not represent my employer's view in any way.