sexta-feira, 27 de setembro de 2013

Very simple way to transform HTML into Excel - MVC3

Hi!

I was wondering about how to transform HTML into Excel format using Razor. I've been searching on the Internet and a came out with the solution showed on picture 1.

1 - Create a view called Report.cshtml

2 - In the controller I created two methods

        public string RenderRazorViewToString(string viewName, object model)
        {
            ViewData.Model = model;
            using (var sw = new StringWriter())
            {
                var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
                var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
                viewResult.View.Render(viewContext, sw);
                return sw.GetStringBuilder().ToString();
            }
        }


        public ActionResult TerritoriesInExcel(string searchString)
        {
            string lStrSearchString = searchString == null ? "" : searchString; ;
            var myTerritories = (new TerritoriesDAO()).getAll(1, true, lStrSearchString);
            Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment;filename=myTerritories.xls");
            Response.ContentType = "application/excel";   

            Response.Write(RenderRazorViewToString("Report",myTerritories));
            Response.End();
            return View(myTerritories);

        }

3 - I have a Index.cshtml in the folder Views

So, the sequence is

Index -> Controller -> Change header response -> Transform Report.cshtml into a string -> make the call -> return to Index.cshtml.

 Two main points: The change that was made to the header of response and the transformation of the view into a string.

Notice that RenderRazorViewToString is a generic function and can be used in whatever context.


picture 1.

References
http://amitpatelit.com/2013/05/22/export-to-excel-in-asp-net-mvc/
http://akinyusufer.blogspot.in/2011/05/razor-render-mvc3-view-render-to-string.html

quarta-feira, 25 de setembro de 2013

Autocomplete - MVC3, RAZOR, LINQ, JQuery

Hi!
This infernal autocomplete took me two days, but I finally made it. Territories is an entity from Northwind schema.

DAO


        public IQueryable<string> getTerritoryDescription(string searchstring)
        {

            NorthwindDataContext lObjND = new NorthwindDataContext();
            var suggestions = from p in lObjND.Territories select p.TerritoryDescription;
            var namelist = suggestions.Where(n => n.ToLower().StartsWith(searchstring.ToLower()));
            return namelist;
        }

CONTROLLER


        public JsonResult AutoCompleteSuggestions(string term)
        {
            var suggestions = (new TerritoriesDAO()).getTerritoryDescription(term);
            var namelist = suggestions.Where(n => n.ToLower().StartsWith(term.ToLower()));
            return Json(namelist, JsonRequestBehavior.AllowGet);
        }

Attention: if you try to use another parameter name instead of term, it will not work. Something like (string searchstring) doesn't work.

VIEW

<script src="../../Scripts/jquery-ui-1.8.11.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-ui-1.8.11.min.js" type="text/javascript"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>

<script type="text/javascript">
    $(function () {
        $("#SearchString").autocomplete({
            source: "/Territories/AutocompleteSuggestions",
            minLength: 1  });
    });
</script>

.
.
@using (Html.BeginForm())
{
    <p>
        Find by name: @Html.TextBox("SearchString") 
        <input type="submit" value="Search" /></p>
}


And that's it!

LINQ - Can't use predicate when joining two or more tables

Hi!

Scenario
Imagine that you're trying (like me) to write a "dynamic" where to your select


            var myTerritories = (from te in lObjDC.Territories
                                 join re in lObjDC.Regions
                                     on te.RegionID equals re.RegionID
                                 select new
                                 {
                                     TerritoryID = te.TerritoryID,
                                     TerritoryDescription = te.TerritoryDescription,
                                     RegionDescription = re.RegionDescription
                                 }).Where(w => w.TerritoryDescription.Contains(pStrSearchString));

To accomplish such thing you'll have to use predicate concept.

var predicate = PredicateBuilder.True<Territory>();

The problem is:
When you execute join using link, it's gonna result in a anonymous type of object. But to build a predicate you need to reference an object. So, if I try something like that:

var predicate = PredicateBuilder.True<Territory>();
predicate = predicate.And(e => e.TerritoryDescription.ToLower().Contains(pStrSearchString.ToLower().Trim()));

            var myTerritories = (from te in lObjDC.Territories
                                 join re in lObjDC.Regions
                                     on te.RegionID equals re.RegionID
                                 select new
                                 {
                                     TerritoryID = te.TerritoryID,
                                     TerritoryDescription = te.TerritoryDescription,
                                     RegionDescription = re.RegionDescription
                                 }).Where(predicate );

pStrSearchString - it's a parameter variable from my code which I borrow and wrote here.

I'm gonna receive  a design mode error. Why? Because the predicate is using Territory as reference. But the result of the select is anonymous.

Solution
So if I can't use predicate when it comes to join, what can I do? Well, why don't you create a view v_Territory . The view will resolve the problem. With that in mind, the select will result in something like taht:

var predicate = PredicateBuilder.True<Territory>();
predicate = predicate.And(e => e.TerritoryDescription.ToLower().Contains(pStrSearchString.ToLower().Trim()));

            var myTerritories = lObjDC.Territories.Where(predicate);

Remember that your view will be something like that

select a.TerritoryID, a.TerritoryDescription, b.RegionDescription
from Territories a
join Region b
on a.RegionID = b.RegionID

Why all of this explanation about predicate? If you are a more or less experienced programmer, you already know that dynamic where will be eventually necessary.

If you don't have the class PredicateBuilder built-in. Here is the code:

using System;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;

public static class PredicateBuilder
{
    public static Expression<Func<T, bool>> True<T>() { return f => true; }
    public static Expression<Func<T, bool>> False<T>() { return f => false; }

    public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1,
                                                        Expression<Func<T, bool>> expr2)
    {
        var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
        return Expression.Lambda<Func<T, bool>>
              (Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);
    }

    public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1,
                                                         Expression<Func<T, bool>> expr2)
    {
        var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
        return Expression.Lambda<Func<T, bool>>
              (Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
    }
}

terça-feira, 24 de setembro de 2013

MVC3-RAZOR-LINQ :: searchstring appears null - ERROR

There are many examples of AutoComplete funcionality on the internet like this one bellow

This is a typical method of a controler

        public JsonResult AutoCompleteSuggestions(string searchstring)
        {
            var suggestions = (new TerritoriesDAO()).getTerritoryDescription(term);
            var namelist = suggestions.Where(n => n.ToLower().StartsWith(term.ToLower()));
            return Json(namelist, JsonRequestBehavior.AllowGet);
        }

Problem is: when the method is called by the view, search string always comes null.

Debugging on firefox you're gona find out that the parameter name is term. So, if you name it as searchstring, it will always be null. Why this happens? I have no idea.

--
[16:21:27.306] GET http://localhost:3289/Territories/AutocompleteSuggestions?term=w [HTTP/1.1 200 OK 0 ms]

--



Change the parameter name to term and magically the value will appear!

        public JsonResult AutoCompleteSuggestions(string term)
        {
            var suggestions = (new TerritoriesDAO()).getTerritoryDescription(term);
            var namelist = suggestions.Where(n => n.ToLower().StartsWith(term.ToLower()));
            return Json(namelist, JsonRequestBehavior.AllowGet);
        }

segunda-feira, 23 de setembro de 2013

Dynamic OrderBy using LINQ

It took me hours to figure out how to make dynamic orderby, using LINQ. It's not easy at all.
There are a lot of abstraction which almost fried my brain.

So let's do it step by step.

As schema of database I'm using northwind.
The table I'm using is Territory.
But like me, you probably came to the conclusion that using the model created by LINQ through database bind is not a good idea. So, I created a class called TerritoryModel. This class is in the folder RAZOR. That's why you're seeing  RAZOR.Models.TerritoryModel as the type of the IOrderedEnumerable result.

Notice that the result of the method getAll is a IOrderedEnumerable and not a IQueryable, which would be expected.

1 -  Variables to define.

            //The LINQ context
            NorthwindDataContext lObjDC = new NorthwindDataContext();


           
            //The function you'll need to dynamically change the field by which you're going to order your result.
            Func<TerritoryModel, object> selector = null;



           
            //The variable which will receive the result of the ordered result.
            IOrderedEnumerable<TerritoryModel> lMT = null;

2 - Swtich

            //I wrote a very simple switch. This is the part of the program that does the trick of dynamically change the column which will order the table Territory.

            #region Selector
            switch (pIntSortBy)
            {
                case 1:
                        selector = a => a.TerritoryID;
                    break;
                case 2:
                        selector = a => a.TerritoryDescription;
                    break;
                case 3:
                        selector = a => a.RegionDescription;
                    break;
            }


3 - The IQueryable select

            var myTerritories = (from te in lObjDC.Territories
                                 join re in lObjDC.Regions
                                     on te.RegionID equals re.RegionID
                                 select new
                                 {
                                     TerritoryID = te.TerritoryID,
                                     TerritoryDescription = te.TerritoryDescription,
                                     RegionDescription = re.RegionDescription
                                 });

4 -The orderBy. An IOrderedEnumerable result.

            if (pBlnIsAsc)
            {
                lMT = myTerritories.Select(p => new TerritoryModel(p.TerritoryID, p.TerritoryDescription, p.RegionDescription)).OrderBy(selector);
            }
            else
            {
                lMT = myTerritories.Select(p => new TerritoryModel(p.TerritoryID, p.TerritoryDescription, p.RegionDescription)).OrderByDescending(selector);
            }

 Why am'I working with IOrderedEnumerable? Well, in the moment you type OrderBy or OrderByDescending, it stops to be IQueryable and starts to be IOrderedEnumerable.

In the end, we'll have a function like that:

 
        public IOrderedEnumerable<RAZOR.Models.TerritoryModel> getAll(int pIntSortBy, bool pBlnIsAsc)
        {
            NorthwindDataContext lObjDC = new NorthwindDataContext();

            Func<TerritoryModel, object> selector = null;
            IOrderedEnumerable<TerritoryModel> lMT = null;
           
            #region Selector
            switch (pIntSortBy)
            {
                case 1:
                        selector = a => a.TerritoryID;
                    break;
                case 2:
                        selector = a => a.TerritoryDescription;
                    break;
                case 3:
                        selector = a => a.RegionDescription;
                    break;
            }
            #endregion          

            var myTerritories = (from te in lObjDC.Territories
                                 join re in lObjDC.Regions
                                     on te.RegionID equals re.RegionID
                                 select new
                                 {
                                     TerritoryID = te.TerritoryID,
                                     TerritoryDescription = te.TerritoryDescription,
                                     RegionDescription = re.RegionDescription
                                 });

            if (pBlnIsAsc)
            {
                lMT = myTerritories.Select(p => new TerritoryModel(p.TerritoryID, p.TerritoryDescription, p.RegionDescription)).OrderBy(selector);
            }
            else
            {
                lMT = myTerritories.Select(p => new TerritoryModel(p.TerritoryID, p.TerritoryDescription, p.RegionDescription)).OrderByDescending(selector);
            }
           
            return lMT;
        }

Et voilà!

sexta-feira, 20 de setembro de 2013

Can't create new controller, "No model classes are available"

Don't worry.
Rebuild your project after you create model and it will be available to create the view and the controller.
That's it.

Razor editor (sweet)

Online, browser-based editor
RazorPad is hosted online at http://razorpad.net. The online editor provides a
sandbox for you to test out your Razor snippets and see the resulting rendered
output right in your browser.

Stand-alone application
For those who prefer “rich clients,” RazorPad is also available as a small standalone
WPF application that does not require any installation. You can download
the RazorPad WPF application from http://razorpad.codeplex.com.

quinta-feira, 19 de setembro de 2013

LINQ - Join tables and populate model class

Hi!

I was wandering how to join one or more tables and populate my enumerable result with my class model.

My Class Model - TerrytoryModel
Table - Territory, Region

1 - The result of the join is a anonymous enumerable list.
2 - Populate a class with the result.



            var myTerritories = (from te in lObjDC.Territories
                                 join re in lObjDC.Regions
                                     on te.RegionID equals re.RegionID
                                 select new
                                 {
                                     TerritoryID = te.TerritoryID,
                                     TerritoryDescription = te.TerritoryDescription,
                                     RegionDescription = re.RegionDescription
                                 }
                                 ).Select(p => new TerritoryModel(p.TerritoryID, p.TerritoryDescription, p.RegionDescription));



3 - The result of myTerritories is a Enumerable list of TerritoryModel.



PS:
Territory and Region are tables from the Northwind database.

MVC2 - How to change the property of a textbox

Hi!

This is a very simple example of how to change the property of a textobox. The code bellow is related to a EDIT functionality.

                <%: Html.TextBoxFor(model => model.CustomerID, new { @readonly = "true", @disable = "true", size = 5, maxlength = 5, style = "background-color:yellow"})%>

  Very simple. Isn't it?



<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication5.Models.CustomerModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Edit
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Edit</h2>

    <% using (Html.BeginForm()) {%>
        <%: Html.ValidationSummary(true) %>
       
        <fieldset>
            <legend>Fields</legend>
           
            <div class="editor-label">
                <%: Html.LabelFor(model => model.CustomerID) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.CustomerID, new { @readonly = "true", @disable = "true", size = 5, maxlength = 5, style = "background-color:yellow"})%>                <%: Html.ValidationMessageFor(model => model.CustomerID) %>
            </div>
           
            <div class="editor-label">
                <%: Html.LabelFor(model => model.CompanyName) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.CompanyName) %>
                <%: Html.ValidationMessageFor(model => model.CompanyName) %>
            </div>
           
            <p>
                <input type="submit" value="Save" />
            </p>
        </fieldset>

    <% } %>

    <div>
        <%: Html.ActionLink("Back to List", "Index") %>
    </div>

</asp:Content>

Há!

ITILV3 - Day 39


ITIL Service Lifecycle

The ITIL Service Lifecycle is a public framework and standard that defines the five stages that service goes through during its life -

Service Strategy, 
Service Design, 
Service Transition, 
Service Operation, 
and Continual Service Improvement.

All five stages of the ITIL Service Lifecycle are focused on the value the IT services deliver.

Value is the heart of the service concept. A service should always deliver Value to customers.

terça-feira, 17 de setembro de 2013

Retrieving data using LINQ, creating and populating object at the same time

 Hi!

I found this way of create and populate object and, at the same time, retrieve data using LINQ.


        public CustomerModel find(string id)
        {
            DataClasses1DataContext lObjDC = new DataClasses1DataContext();

            var varCustomerModel = lObjDC.Customers.Where(x => x.CustomerID == id).Select(p => new CustomerModel(p.CustomerID, p.CompanyName)).Single();

            return varCustomerModel;
        }

Insert using LINQ

 Hi!

This is a method from a project I'm currently working at.

        public bool insert(string id, string companyName)
        {
            DataClasses1DataContext lObjDC = new DataClasses1DataContext();

            int lCount = lObjDC.Customers.Where(x => x.CustomerID == id).Count();




            //If customer doesn't exist.
            if (lCount == 0)
            {
                Customer lObjCTM = new Customer();
                lObjCTM.CustomerID = id;
                lObjCTM.CompanyName = companyName;
                lObjDC.Customers.InsertOnSubmit(lObjCTM);
                lObjDC.SubmitChanges();

                return true;
            }
            return false;
        }


PS: I should have written an exist method instead of using Count() inside the insert method.

Update using LINQ

 Hi!

This is a method from a project I'm currently working at.

        public void update(string id, string companyName)
        {
            DataClasses1DataContext lObjDC = new DataClasses1DataContext();
            var lObjCustomer = lObjDC.Customers.Where(x => x.CustomerID == id).Single();
            lObjCustomer.CompanyName = companyName;
            lObjCustomer.CustomerID = id;
            lObjDC.SubmitChanges();         
        }

terça-feira, 3 de setembro de 2013

FTP DOS

If you are using windows as an OS you probably have the ftp (file transfer protocol) available for you to use.

How to access it via DOS?

Very simple.

1 - Go to the start window and type cmd

2 - At the DOS prompt type ftp

3 - Open [the ip address of the other computer you want to access]

4 - Type the user

5 - Type the password

6 - You're logged in.




The commands


!               delete          literal         prompt          send
?               debug           ls              put             status
append          dir             mdelete         pwd             trace
ascii           disconnect      mdir            quit            type
bell            get             mget            quote           user
binary          glob            mkdir           recv            verbose
bye             hash            mls             remotehelp
cd              help            mput            rename
close           lcd             open            rmdir


segunda-feira, 2 de setembro de 2013

ITILV3 - Day 38


IT Service Continuity Management (ITSCM)

IT Service Continuity strategy is based on:

1. Risk assessment
2. Business continuity strategy
3. Business Impact Analysis

ITSCM analyzes and controls the risks that could affect IT Services

A risk is a possibility that something bad might happen as a result of something else happening.

Example: If I build a an oceanfront house, there is a risk that a hurricane could destroy it.

Risk Management ensures that:

1 - Operations continue after a major event. (An earthquake?)
2 - Assets (information, facilities and building) are protected.
3 - Workplace is safe for emplyees and customers.

Some times it seems to me that ITIL says, over and over again, the same thing, only changing the order of the words.