Wednesday, December 7, 2011

MVC 3 Razor And Partial View

MVC 3 Razor And Partial View

How do I use partial view in MVC 3?
Great!! Sometime, I don’t know why, I stuck for small things. I was trying to load two partial views(MVC 3). But, after spending 6 hours I got to know how to load.
This retards my brain. I have searching for good articles on MVC 3 & Partial views. But, I failed to get it.
So, Let us go in dept on MVC3 & Partial Views.
Partial view and User Controls are somewhat same in purpose of usage. But the way in which they is totally different. I will explain about these in detail.

Step 1
Create ASP.Net MVC 3 project using VS 2010. I have named it as ARVPartialView.
Add one partiaview and name it as PartialIndex












Step2
I have created one module like PartialModel as given bellow.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ARVPartialView.Models
{
public partial class PartialModel
{
public string Name { get; set; }
public int Actual { get; set; }
public int Target { get; set; }
public int Score { get; set; }
}

public partial class PartialModel
{
public List lstPartialModel { get; set; }

}
}




Step 3

Now the time to add some values to this list. The solution structure looks like as given bellow.




Since I like render partial view over Home/Index, So, I have written one function in HomeController.

public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
///
/// TODO:To get data and returning result to the index view..
///

///
return View(new PartialModel() { lstPartialModel = GetSampleData() });

}

public ActionResult About()
{
return View();
}


///
/// TODO:Function to add some data to list.
///

///
private List GetSampleData()
{
List model = new List();

model.Add(new PartialModel()
{

Name = "Aravind",
Actual = 10000,
Target = 12000,
Score = 83
});

model.Add(new PartialModel()
{

Name = "Ram",
Actual = 8000,
Target = 14000,
Score = 57
});
model.Add(new PartialModel()
{
Name = "Ajay",
Actual = 50000,
Target = 35000,
Score = 143
});

return model;
}
}


Step 4

In Index view, I have included partial view as given bellow. As like web usercontrol, partial view doest initiate any events while loading Just like initializecomponet etc. So, we have to pass the list to partial view in the parent page.







Step 5
Render partial view on Index view.




Step 6


Run the application.



Have a nice time & enjoy..
aravindbenator@gmail.com
http://silverlightarvind.blogspot.com







Monday, December 5, 2011

What is Silverlight Application Library Caching?

What is Silverlight Application Library Caching?


I was just working with one Silverlight application that has too many 3rd party assemblies (Excel, theme, charts etc).

Also it was taking long time while downloading first time. Obviously, this is because of large XAP size. So, I thought to divide entire application in multiple projects which will create multiple XAP’s and then use it by on demand load.

But, I have used more third party dll’s. In this case it is too hectic to store them on separate XAP and then load it on demand. Then, I came to know about new concept (for me) about “ Application Library Caching”



So, What is Assembly caching? We all know about the on demand download of XAP file. To do this, we need to write code for downloading the external xap files using WebClient. Application Library caching does the similar thing for you very easily. Suppose, if you have a bigger application and used a huge 3rd party libraries, this easy step will help you make a separate zip file with them which can be downloaded on demand without writing any additional code.



Step 1.

Create one Silverlight 3 or 4 application(its available in SL 3 also). I have named it as ARVApplicationLibraryCaching.

If you see in the solution-references, you will find the assemblies ,those have been added to the project.





Once you build the app, you can find XAP file in the client bin directory.

To see the contents inside the XAP, go to the ClientBin folder. There you will find the XAP output of your Silverlight application. Rename it with zip and extract then, you can find as given bellow.




To see all the assembly references, open appmanifest file using notepad.




You will notice that, it has all the referenced dll information as Assembly part, entry point and required runtime version. Assembly parts tells to load the referred assembly directly from the XAP.





Step 2.

Adding more third party Assemblies.

Go to add reference and add some third party dll’s. I have added here some dll’s related to theme.





Once you build the app and unzip the XAP. Then you will find as given bellow.




You will see that all the new referenced dlls are now part of your XAP. If you open AppManifest.xmal,






it contains all those additional dll entries as AssemblyPart and tells to load them from the XAP itself.

This actually increases the XAP size and makes your application little bulky. Thus increases initial loading time of your application.



Step 3.

Use application library caching.

To overcome this, there is a setting project property window as shown bellow.






Now, build the project. You will find zip files as per you have been added third party dll’s.




Then, go to AppManifest file, then see






Here a single AssemblyPart pointing to your original project output and rest of the dll references are now moved to ExternalParts. They are now pointing to the Zip files instead of the actual dlls.

Now, when you run your application, it will only download the XAP file which contains only the main resources. Rest of the dll ZIP will be downloaded on demand whenever require by your application.

Enjoy the concept!!!!!!!



Aravind