Rich

Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, October 14, 2010

[C# Asp.net MVC2] Paging with MVC Contrib

In a MVC application may need to page the results, or maybe the result of a query returns several hundred results, and we want to do on the web page view 10 per page and navigate between pages.
It is not a complicated thing, indeed, is very simple, whether done by "our code, or by using third-party libraries. one of these libraries is MVC Contrib.
In this post I will talk about the MVC Contrib, a project to add functionality and ease of use the Microsoft ASP.NET MVC Framework, which is useful for developers looking to develop and test the user interface elements, particularly through the use of MVC Contrib Grid, "Grid" for paging.
Then, download the library, includiamola in the project, and in our controller we call the namespace you need:


using MvcContrib.Pagination;
using MvcContrib.UI.Grid;
using MvcContrib;
using MvcContrib.UI.Pager;

Imagine having to see a list of Customer objects, retrieved from a repository, defined as:

public class Customer
{
   public int Id { get; set; }
   public string Surname { get; set; }
   public string Forename {get; set; }
   public DateTime DateOfBirth { get; set; }
}

In the controller :

public class CustomerController : Controller 
{
   private CustomerRepository _customRep = new CustomerRepository();

    public ActionResult Index(int? page)
   {
     var pagedCustomers = _customRep.FindAll().AsPagination(page ?? 1, 10);
     return View(pagedCustomers);
   }

View on the other hand, we import the namespaces you will need:

<%@ Import Namespace="MvcContrib.UI.Grid" %>
<%@ Import Namespace="MvcContrib.UI.Grid.ActionSyntax" %>
<%@ Import Namespace="MvcContrib.Pagination"%>
<%@ import Namespace="MvcContrib.UI.Pager" %>

 and build the grid:

<%= Html.Grid(Model).Columns(column => {
  column.For(cust => cust.Id).Named("Custome ID").DoNotEncode();
  column.For(cust => cust.Surname).Named("Surname").DoNotEncode();
  column.For(cust => cust.DateOfBirth).Named("Date Of Birth").DoNotEncode();
}) %>

<%= Html.Pager((IPagination)Model) %>


This is just a litle example as a page that shows a simple visualization of results, naturalemnte can be inserted into the "columns" of not only data but other things like ActionLink, for example:

column.For(cust => Html.ActionLink(cust.Id, "Details", new { id = cust.Id })).Named("Customer ID").DoNotEncode();

instead of showing only the id, creates a link to ActionResult "Details" passing the customer id as a parameter;

or, we assume that the customer has an additional property called "Photo" is inserted where the address of a picture:


column.For(cust => Html.ActionLinkPhoto(cust.Id, cust.Photo, "Details", new { id = cust.Id })).Named("Customer ID").DoNotEncode();

where ActionLinkPhoto Helper is a class in action specially written for a link where instead of having a "written", you have an image.

Thursday, October 7, 2010

[c# .net asp.net MVC2] Import contacts gmail

In an application, you may need to import contacts from a gmail account.
The construction is very simple and based on the Google Data API, a collection of libraries that provides a simple protocol for reading and writing data from the web.

First, download the GoogleDataApi
(you find here),  install them on your PC and add them as a reference to your project (in this case we can use Google.GData.Contacts.dll, Google.GData.Client.dll, Google.GData.Extensions.dll).
Importing different namespace


using Google.GData.Contacts;
using Google.GData.Client;
using Google.GData.Extensions;

Now you are ready to implement the code to read your Gmail contacts.

string authSubUrl = AuthSubUtil.getRequestUrl("http://www.example.com/Hello.asp", "http://www.google.com/m8/feeds/", False, True);

GAuthSubRequestFactory authFactory = new GAuthSubRequestFactory("cp", "exampleCo-exampleApp-1");
authFactory.Token = (String)Session["token"];
ContactsService service = new ContactsService("exampleCo-exampleApp-1");
  service.setUserCredentials(email, password);
ContactsQuery query = new ContactsQuery(ContactsQuery.CreateContactsUri("default"));
ContactsFeed feed = service.Query(query);




At this point in the feed.Entries there are contacts.
enough to extrapolate a simple loop to read through all the contacts, for example:


foreach (ContactEntry item in feed.Entries)

{

      // in item.Name we have various "names" of the contact, for example:
      //item.Name.FullName
      //item.Name.FamilyName
      //item.Name.GivenName
      //item.Name.NamePrefix
      //item.Name.NameSuffix
      //item.Name.AdditonalName
     // item.Emails while inside we have all the addresses associated with the various properties,
    // So here just a simple read cycle to extract all the data:

             foreach (EMail email in entry.Emails)

                {
                    // Email.Address you get in your email address
                    
// In email.Home, email.Work, email.Other is a bool that
                    
// Indicate if this address is
                    
// Classified as Home, Work or Other

                 }
}

Of course this is just an example of using these bees, for the simple extrapolation of the contacts from Gmail, the functions provided are varied and you can do much more.

Program PTC...earning reading advertise