Translate

Wednesday, 25 November 2015

Static and Dynamic Droprdownlists in MVC

Here How to bind Dropdown in Static and Dynamically.


Static DropDownList:
  
            @Html.DropDownListFor(m=>m.CountryName,new List<SelectListItem>{
                    new SelectListItem{ Text="India", Value = "1" },
                    new SelectListItem{ Text="US", Value = "0" },
new SelectListItem{ Text="UK", Value = "2" }
                 })

Dynamic DropDownList:

  @Html.DropDownListFor(m=>m.CountryName,new SelectList((System.Collections.IEnumerable)ViewData["country"],"value","text"),"Select")

          

Friday, 20 November 2015

Dynamically shortened Text with “Show More” link using jQuery

To Show Some text and after cleikc on read more show all text

<html>
<head>
    <script src="http://code.jquery.com/jquery-1.8.2.js" type="text/javascript"></script>
    <title>jQuery Add More/Less link to Text</title>
    <script type="text/javascript">
        $(function () {
            var showChar = 120, showtxt = "Read More", hidetxt = "Readless";
            //  $('.more').each(function () {
            var content = $('.more').text();
            if (content.length > showChar) {
                var con = content.substr(0, showChar);
                var hcon = content.substr(showChar, content.length - showChar);
                var txt = con + '<span class="dots">...</span><span class="morecontent"><span>' + hcon + '</span>&nbsp;&nbsp;<a href="" class="moretxt">' + showtxt + '</a></span>';
                $('.more').html(txt);
            }
            //  });
            $(".moretxt").click(function () {
                if ($(this).hasClass("sample")) {
                    $(this).removeClass("sample");
                    $(this).text(showtxt);
                } else {
                    $(this).addClass("sample");
                    $(this).text(hidetxt);
                }
                $(this).parent().prev().toggle();
                $(this).prev().toggle();
                return false;
            });
        });
    </script>
    <style type="text/css">
        body {
            font-family: Calibri, Arial;
            margin: 0px;
            padding: 0px;
        }

        .more {
            width: 400px;
            background-color: #f0f0f0;
            margin: 10px;
        }

        .morecontent span {
            display: none;
        }
    </style>
</head>
<body>
    <div class="more">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum laoreet, nunc eget laoreet sa agittis, quam ligula sodales orci, congue imperdiet eros tortor ac lectus. Duis eget nisl orci. Aliquam mattis purus non mauris blandit id luctus felis convallis. Integer varius egestas vestibulum. Nullam a dolor arcu, ac tempor elit. Donec.
    </div>

</body>
</html>

SeeImage





Wednesday, 18 November 2015

How to export List Data to EXCEL,Word,PDF..

Export List data to Excel, Word, Pdf is shown below.

In Controller:
Getting data :

public ActionResult jQGRID()
        {
            Sample sample = new Sample();
            List<Sample> lst = new List<Sample>();
            lst = sample.GetList();
            Session["Export"] = lst;
            return View(lst);
        }



in view:



<input type="button" id="Exporttopdf" value="Exporttopdf" />
<input type="button" id="ExporttoExcel" value="Export to Excel" />
<input type="button" id="ExporttoWord" value="Export to Word" />

onclick functions using Jquery:

<script>
    $(document).ready(function () {
        $("#Exporttopdf").click(function () {
            window.location = '@Url.Action("ExportGridToPDF","JqGrid")';
        });
        $("#ExporttoExcel").click(function () {
            window.location = '@Url.Action("ExportToExcel","JqGrid")';
        });
        $("#ExporttoWord").click(function () {
            window.location = '@Url.Action("ExportToWord","JqGrid")';
        });
        
    });
</script>

Methods when click on buttons to export in controller:


public void ExportToExcel()
        {
            if (Session["Export"] != null)
            {
                GridView gv = new GridView();
                gv.DataSource = Session["Export"];
                gv.DataBind();
                Response.ClearContent();
                Response.Buffer = true;
                Response.AddHeader("content-disposition", "attachment; filename=Kavitha.xls");
                Response.ContentType = "application/ms-excel";
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());
                Response.Charset = "";
                StringWriter sw = new StringWriter();
                HtmlTextWriter htw = new HtmlTextWriter(sw);
                gv.RenderControl(htw);
                Response.Output.Write(sw.ToString());
                Response.Flush();
                Response.End();
            }
        }

        public void ExportGridToPDF()
        {
            if (Session["Export"] != null)
            {
                GridView gv = new GridView();
                gv.DataSource = Session["Export"];
                gv.DataBind();
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=Kavitha.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                StringWriter sw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw);
                gv.RenderControl(hw);
                StringReader sr = new StringReader(sw.ToString());
                Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                pdfDoc.Open();
                htmlparser.Parse(sr);
                pdfDoc.Close();
                Response.Write(pdfDoc);
                Response.End();

            }

public void ExportToWord()
        {
            if (Session["Export"] != null)
            {
                GridView gv = new GridView();
                gv.DataSource = Session["Export"];
                gv.DataBind();
                Response.Clear();
                Response.Buffer = true;
                Response.AddHeader("content-disposition",
                "attachment;filename=Kavitha.doc");
                Response.Charset = "";
                Response.ContentType = "application/vnd.ms-word ";
                StringWriter sw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw);
                gv.RenderControl(hw);
                Response.Output.Write(sw.ToString());
                Response.Flush();
                Response.End();
            }

        }

Check Login session before every action hitting in MVC



Here is checking the session values every time when before hitting to every action.



  protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (Session["User"] != null)
            {
                base.OnActionExecuting(filterContext);
            }
            else
            {
                TempData["TimeOut"] = "Please login again.";
                filterContext.Result = RedirectToAction("Index", "Home", new { area = "" });
            }

        }