Saturday, 27 September 2014

ajax call

 if (document.forms[0].checkValidity()) {
 
                e.preventDefault();
 
                $.ajax({
                    type: "POST",
                    url: Person.SaveUrl,
                    data: ko.toJSON(Person.ViewModel),
                    contentType: 'application/json',
                    async: true,
                    beforeSend: function () {
                        // Display loading image
                    },
                    success: function (result) {
                        // Handle the response here.
                    },
                    complete: function () {
                        // Hide loading image.
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        // Handle error.
                    }
                });
 
            }
 

Friday, 12 September 2014

Multilevel Menu in mvc 3.0

table structure

MenuId    MenuName    MenuOrder    MainMenuId    ControllerName    ActionName    LinkName
1               Home                   1               NULL                   Controller1          Index               Home
2             Microsoft              2               NULL                   Controller2           Index           Microsoft
3             Windows               1                 2                          Controller1           Index            Windows
4             WindowsXP           1                3                           Controller1          Index          Windows XP
5               Windows7            2                3                            Controller2          Index         Windows 7
6            Windows8              3                3                            Controller1           Index         Windows 8
7              MS Office            2                2                             Controller1          Index          MS Office
8               Apple                 3                NULL                     Controller1          Index            Apple
9            IPhone                  1                   8                            Controller1          Index             IPhone
10          IPad                      2                8                             Controller1          Index             IPad
11          MacAir                 3                8                              Controller1         Index           Mac AirNot

=========================================================================
controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MultilevelMenu.Models;

namespace MultilevelMenu.Controllers
{
    public class MenuController : Controller
    {
        MenuEntities MEnt = new MenuEntities();

        [ChildActionOnly]
        public ActionResult Menus()
        {
            var mMenu = MEnt.Menu.ToList();
            return PartialView(mMenu);
        }

    }
}
============================================================
model

using System.Data.Entity;
using System.Data.Mapping;

namespace MultilevelMenu.Models
{
    public class MenuEntities : DbContext
    {
        public DbSet<Menus> Menu { get; set; }
    }
}
==============================================================
Menus.cshtml

@model IEnumerable<MultilevelMenu.Models.Menus>
<div>
    @Html.Raw(@Html.ParentMenus(Model))
</div>
=============================================================
.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace MultilevelMenu.Models
{
    [Table("Menus")]
    public class Menus
    {
        [Key]
        public int MenuId { get; set; }
        public string MenuName { get; set; }
        public int MenuOrder { get; set; }
        public int? MainMenuId { get; set; }
        public string LinkName { get; set; }
        public string ActionName { get; set; }
        public string ControllerName { get; set; }
    }

}
================================================================
htmlhelper class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using MultilevelMenu.Models;

namespace MultilevelMenu.HtmlHelpers
{
    public static class HtmlHelperExtensions
    {
        public static string ParentMenus(this HtmlHelper html, IEnumerable<Menus> menu)
        {
            string htmlOutput = string.Empty;

            if (menu.Count() > 0)
            {
                htmlOutput += "<ul class='sf-menu'>";
                var MainMenu = from mainMenu in menu where mainMenu.MainMenuId == null orderby mainMenu.MenuOrder select mainMenu;
                foreach (Menus m in MainMenu)
                {
                    htmlOutput += "<li>";
                    htmlOutput += LinkExtensions.ActionLink(html, m.LinkName, m.ActionName, m.ControllerName);
                    htmlOutput += SubMenus(html, menu, m.MenuId);
                    htmlOutput += "</li>";
                }
                htmlOutput += "</ul>";
            }
            return htmlOutput;
        }

        private static string SubMenus(this HtmlHelper html, IEnumerable<Menus> SubMenu, int MenuId)
        {
            string htmlOutput = string.Empty;
            var subMenu = from sm in SubMenu where sm.MainMenuId == MenuId orderby sm.MenuOrder select sm;
            if (subMenu.Count() > 0)
            {
                htmlOutput += "<ul>";
                foreach (Menus m in subMenu)
                {
                    htmlOutput += "<li>";
                    htmlOutput += LinkExtensions.ActionLink(html, m.LinkName, m.ActionName, m.ControllerName);
                    htmlOutput += SubMenus(html, SubMenu, m.MenuId);
                    htmlOutput += "</li>";
                }
                htmlOutput += "</ul>";
            }
            return htmlOutput;
        }

        //private static string SubMenus(IEnumerable<Menus> iEnumerable)
        //{
        //    throw new NotImplementedException();
        //}
    }
}
=================================================================
Output like this:

Multilevel Menu


Saturday, 6 September 2014

working with google chart in mvc

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Mvcchart.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            return View();
        }

        public JsonResult GetData()
        {
            return Json(CreateCompaniesList(), JsonRequestBehavior.AllowGet);
        }

        private IEnumerable<Company> CreateCompaniesList()
        {
            List<Company> companies = new List<Company>();

            Company company = new Company() { Expense = 1200, Salary = 2000, Year = new DateTime(2012, 1, 1).ToString("yyyy/MM") };
            Company company1 = new Company() { Expense = 1300, Salary = 2100, Year = new DateTime(2012, 2, 1).ToString("yyyy/MM") };
            Company company2 = new Company() { Expense = 1240, Salary = 2000, Year = new DateTime(2012, 3, 1).ToString("yyyy/MM") };
            Company company3 = new Company() { Expense = 1100, Salary = 3300, Year = new DateTime(2012, 4, 1).ToString("yyyy/MM") };
            Company company4 = new Company() { Expense = 140, Salary = 1100, Year = new DateTime(2012, 5, 1).ToString("yyyy/MM") };
            Company company5 = new Company() { Expense = 1500, Salary = 1900, Year = new DateTime(2012, 6, 1).ToString("yyyy/MM") };

            companies.Add(company);
            companies.Add(company1);
            companies.Add(company2);
            companies.Add(company3);
            companies.Add(company4);
            companies.Add(company5);

            return companies;
        }

     
    }
}
=================================================================
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script src="../../Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        google.load("visualization", "1", { packages: ["corechart"] });
        google.setOnLoadCallback(drawChart);
        function drawChart() {
            $.get('/Home/GetData', {},
    function (data) {
        var tdata = new google.visualization.DataTable();

        tdata.addColumn('string', 'Year');
        tdata.addColumn('number', 'Salary M');
        tdata.addColumn('number', 'Expense');

        for (var i = 0; i < data.length; i++) {
            tdata.addRow([data[i].Year, data[i].Salary, data[i].Expense]);
        }

        var options = {
            title: "mukesh Salary For the current year"
        };

        var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
        chart.draw(tdata, options);
    });
        }
    </script>
</head>
<body>
    <div>
        <div id="chart_div" style="width: 900px; height: 500px;">
        </div>
    </div>
</body>
</html>
======================================================================
class



public class Company
    {
       public int  Expense {get;set;}
        public int  Salary {get;set;}
        public string Year { get; set; }       
    }
======================================================
Result


mukesh Salary For the current yearSalary MExpense2012/012012/022012/032012/042012/052012/0601,0002,0003,0004,000

Monday, 1 September 2014

Using Group by in Entity Framework

we have emp table  and department table

EmpID    EmpName    salary
1    Mukesh                 3000.00
2    Rakesh                  3000.00
3    Raghav                  3000.00
4    Sunil                     3000.00
5    radhika                 3000.00
6    gajraj singh          3000.00
7    akshay                  3000.00
8    anuradha             3000.00
9    hari                     3000.00
10    dee                    3000.00
11    dee                    3000.00
12    dee                   3000.00
13    ajay ml            3000.00
14    sudama           3000.00
15    raju                3000.00
16    iti                  3000.00
17    ravi               3000.00 
==========================================================
department


Depid    Depname    Address1    City    EmpID
1    IT    B/1 new delhi    delhi    4
2    Software    Mamura    Noida    2
3    Software    sector 15    Noida    3
4    Software    shuklaganj    Kanpur    1
5    Medical    Babina    jhansi    7
6    Railway    Babina    Jhansi    8
7    Railway    Babina    Jhansi    9
8    Railway    Babina    Jhansi    10
9    Railway    Babina    Jhansi    11
10    Railway    Babina    Jhansi    12
11    Railway    Babina    Jhansi    13
12    Railway    Babina    Jhansi    14
13    Railway    Babina    Jhansi    15
14    Railway    Babina    Jhansi    16
15    Railway    Babina    Jhansi    17


==================================================================

CountryEntities obj = new CountryEntities();
            var datalist = (from s in obj.Employees
                            join sl in obj.Departments on s.EmpID equals sl.EmpID
                            group s by new { s.EmpName, sl.Depname } into g

                            select new { department = g.Key.Depname, empName = g.Key.EmpName, totalsalary = g.Sum(P => P.salary) });

            GridView1.DataSource = datalist;
            GridView1.DataBind();
=================================================================
output
here output will come on department and name basis


departmentempNametotalsalary
ITSunil3000.00
Medicalakshay3000.00
Railwayajay ml3000.00
Railwayanuradha3000.00
Railwaydee9000.00
Railwayhari3000.00
Railwayiti3000.00
Railwayraju3000.00
Railwayravi3000.00
Railwaysudama3000.00
SoftwareMukesh3000.00
SoftwareRaghav3000.00
SoftwareRakesh3000.00




using object Parameter in Entity Framework

     [HttpPost]
        public ActionResult About(Employee1 onjemp)
        {
            int k=0;
            if (ModelState.IsValid)
            {
                System.Data.Objects.ObjectParameter output = new System.Data.Objects.ObjectParameter("CustomerCount", typeof(int));
               
               
                Employee emp = new Employee();
               // emp.EmpName = onjemp.EmpName;
               // objenties.Employees.AddObject(emp);

                k = objenties.insert(onjemp.EmpName, "Railway", "Babina", "Jhansi", output);
               // k = objenties.SaveChanges();
                int j =Convert.ToInt32(output.Value);
                if (j> 0)
               {
                   ViewBag.sucess = "Inserted Successfull";
                   Employee1 onjemp1 = new Employee1();
                   return View("~/Views/Home/Index.cshtml", onjemp1);
               }
               else
               {

                   ViewBag.sucess = "Some Problem";
                  
                   return View(onjemp);

               }

               
            }
            else
            {
                return View("~/Views/Home/Index.cshtml", onjemp);
            }
        }