Thứ Năm, 28 tháng 8, 2014

Thuật toán Bubble Sort


Phương pháp nhánh cận

Phương pháp (hay giải thuật) nhánh cận là một trong những phương pháp giải các bài toán liệt kê cấu hình có điều kiện tối ưu.

Thứ Tư, 27 tháng 8, 2014

Using the Grid.MVC in ASP.NET MVC

Introduction 

Most of times we will have to show data in our apps, but we need to provide different features like paging, filtering, sorting and much more, so the Grid.MVC provides amazing tools for that goal when we develop applications using ASP.NET MVC.

Background

This article shows how through the Grid.MVC you can use features like paging, filtering and sorting.

Using the code   

Firts step: create a new ASP.NET MVC 4 application, I recommended to use the basic template.

Next, lets create a new class Client, this class will be the model:
public class Client
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
}
Now that you have the model done, lets create the Client controller. This controller have only on action, and the data source will be a List, but here you can connect to a database, the action returns the data:
public class ClientController : Controller
{
    private readonly List clients = new List()
    {
        new Client { Id = 1, Name = "Julio Avellaneda", Email = "julito_gtu@hotmail.com" },
        new Client { Id = 2, Name = "Juan Torres", Email = "jtorres@hotmail.com" },
        new Client { Id = 3, Name = "Oscar Camacho", Email = "oscar@hotmail.com" },
        new Client { Id = 4, Name = "Gina Urrego", Email = "ginna@hotmail.com" },
        new Client { Id = 5, Name = "Nathalia Ramirez", Email = "natha@hotmail.com" },
        new Client { Id = 6, Name = "Raul Rodriguez", Email = "rodriguez.raul@hotmail.com" },
        new Client { Id = 7, Name = "Johana Espitia", Email = "johana_espitia@hotmail.com" }
    };
 
    public ActionResult Index()
    {
        return View(clients);
    }
} 

Before create the view, will need to add a Grid.MVC package using Nuget:
 
Also add Boostrap:


When the Grid.MVC package is installed, you can found some new views in the Views/Shared folder:

In the scripts folder, tree new JavaScript files will be found:
 
And in the content folder now you see the css file for the Grid:

Next step now is to create a new View for the action Index of the controller Client, like this:
@model IEnumerable<Grid.Models.Client>
@using GridMvc.Html
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <link href="@Url.Content("~/Content/Gridmvc.css")" rel="stylesheet" />
    <link href="@Url.Content("~/Content/bootstrap.min.css")" rel="stylesheet" />
    <script src="@Url.Content("~/Scripts/jquery-1.9.1.min.js")"></script>
    <script src="@Url.Content("~/Scripts/gridmvc.min.js")"></script>
    <title>Index</title>
</head>
<body>
    <h1>Grid.MVC</h1>
    <hr />
    <div style="width:500px;">
        @Html.Grid(Model).Columns(columns => 
                    {
                        columns.Add(c => c.Id).Titled("Client ID");
                        columns.Add(c => c.Name).Titled("Name").Filterable(true);
                        columns.Add(c => c.Email).Titled("Email");
                    }).WithPaging(3).Sortable(true)
    </div>
</body>
</html>
The key parts on the code are:
  • Add: @using GridMvc.Html 
  • Add the references to the css files
  • Add the references to the JavaScript files
To use the Grid, use the HTML Helper @Html.Grid, some important properties for the helper:
  • Titled: The column title
  • Filterable: Define if the column has the feature to be filterable
  • Sortable: Define if the column has the feature to be sortable
  • WithPaging: Define the number of rows to show for page 
Finally, you have the following cool Grid:




I hope this post will be useful for you!

Using TempData Outside the Controller in ASP.NET MVC


2 Jun 2014
Using TempData outside the controller in ASP.NET MVC and making TempData less prone to error and more intuitive.
Is your email address OK? You are signed up for our newsletters but your email address is either unconfirmed, or has not been reconfirmed in a long time. Please click here to have a confirmation email sent so we can confirm your email address and start sending you newsletters again. Alternatively, you can update your subscriptions.

What is the Problem We Are Trying to Fix?

When you start using Tempdata, it quickly becomes evident that maintaining it will be quite a challenge because Intellisense doesn't support Tempdata since it's a Dictionary. Without intellisense, suddenly life looks gloomy, but no fear. There is hope!

My Problem

The problem raised when I wanted to use Notification Object across my website. I wanted to throw this object from one controller to the other (tempdata job) and finally send it to the view. If I wanted to useTempData["something"], then I have to remember this "something".
So instead of hardcoding this "something" inside the controller, I decided to put the hardcoding inside aHelper.cs function and simply call intuitive functions that will Get/Set/Remove the object. The beauty of it is that - there is Intellisense to help us out!

Solution

The solution is quite simple.
  1. Create the Notification object
  2. Create a helper function that will do the dirty work (get;set;remove)
Since we moved TempData outside the controller, we are obliged to send the controller to be able to use theTempData. We send the controller by the keyword this.

PromptNotification.cs

public class PromptNotification //Notification Object
{
    public bool NotificationSuccess { get; set; }
    public string NotificationMessage { get; set; }
}

Helper.cs

public static void SetPromptNotification(Controller controller, bool success,string message)
{
    PromptNotification promptNotification = new PromptNotification
    {
        NotificationSuccess = success,
        NotificationMessage = message
    };
    controller.TempData["PromptNotification"] = promptNotification;
}

public static PromptNotification GetPromptNotification(Controller controller)
{
    try
    {
        PromptNotification promptNotification = controller.TempData["PromptNotification"] 
            as PromptNotification;
        return promptNotification;
     }
     catch (Exception ex)
     {
         // log error here

         return null;
     }
}

public static void RemovePromptNotification(Controller controller)
{
    controller.TempData.Remove("PromptNotification");
}

UserController.cs //set

// add promp message => redirect to index
Helper.SetPromptNotification(this, true, 
    "You will shortly receive an email, please verify your registration.");

MainController.cs //get

// get promptnotification => if it exist => pass it to the view => clear the notification
PromptNotification promptNotification = Helper.GetPromptNotification(this);

if (promptNotification != null)
{
    ViewBag.NotificationSuccess = promptNotification.NotificationSuccess;
    ViewBag.NotificationMessage = promptNotification.NotificationMessage;

    Helper.RemovePromptNotification(this);
}

Index.cshtml

@if (ViewBag.NotificationSuccess != null)
{
    PromptNotification promptNotification = new PromptNotification
    {
        NotificationSuccess = (bool)ViewBag.NotificationSuccess,
        NotificationMessage = ViewBag.NotificationMessage as string
    };
    
    string notificationClassName = String.Empty;

    if (promptNotification.NotificationSuccess == true)
    {
        notificationClassName = "notificationSuccessWrapper";
    }
    else
    {
        notificationClassName = "notificationErrorWrapper";
    }
    
    <div class="@Html.Raw(notificationClassName)">
        @promptNotification.NotificationMessage
    </div>
}