Sunday, 29 January 2017

How to enum class create and call enum data in controller?

Firstly we create a class enum.cs -:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectName.Entity
{
    public class ProjectEnum
    {
        public enum Status : int
        {
            Created = 1,
            CheckedOut = 2,
            CheckedIn = 3,
            Published = 4,
            Deleted = 5
        }

        public enum SourceType : int
        {
            SBI = 1,
           Axis = 2,
            ICICI = 3,
        }
    }
}

int type = Convert.ToInt32(ProjectName.SourceType.SBI );

How to age calculate from date in asp.net?

  private static TimeZoneInfo INDIAN_ZONE = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
public static string GetCalcaulateAge(DateTime date)
        {
            string _age = "";
            int age = 0;
            int month = 0;
            int days = 0;
            int dobYear = Convert.ToInt32(date.Year);
            int dobMonth = Convert.ToInt32(date.Month);
            int dobDay = Convert.ToInt32(date.Day);
            age = Convert.ToInt32(TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, INDIAN_ZONE).Year) - dobYear;
            month = Convert.ToInt32(TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, INDIAN_ZONE).Month) - dobMonth;
            days = Convert.ToInt32(TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, INDIAN_ZONE).Day) - dobDay;

            if (days < 0)
            {
                days = Math.Abs(days);
            }

            if (month < 0)
            {
                month = Math.Abs(month);
            }
            if (age == 0)
            {
                if (month == 0)
                {
                    _age = Convert.ToString(days) + " days";
                }
                else
                {
                    if (days == 0)
                    {
                        _age = Convert.ToString(month) + " months";
                    }
                    else
                    {
                        _age = Convert.ToString(month) + " months " + days + " days";
                    }
                }
            }
            else
            {
                if (age <= 12)
                {
                    if (days == 0)
                    {
                        _age = Convert.ToString(age) + " yrs " + Convert.ToString(month) + " months";
                    }
                    else if (month == 0)
                    {
                        _age = Convert.ToString(age) + " yrs " + days + " days";
                    }
                    else
                    {
                        _age = Convert.ToString(age) + " yrs " + Convert.ToString(month) + " months " + days + " days";
                    }
                }
                else
                {
                    _age = Convert.ToString(age) + " yrs";
                }
            }
            return _age;
        }

Saturday, 28 January 2017

How to autocomplete text box by ajax with filtering in mvc?

<link rel="stylesheet"href="//code.jquery.com/ui/1.12.1/themes/base/jqueryui.css"> <link rel="stylesheet" href="/resources/demos/style.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="textjavascript">
    function changeSourceType(obj) {
        debugger;
        var sourceData = [];
        var sourcetype = document.getElementById("ddlSourceType" + obj).value;
        $("#txtSource" + obj).autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: '/Layout/FillSource',
                    type: "GET",
                    dataType: "JSON",
                    data: { sourcetype: sourcetype },
                    success: function(data) {
                        var array = $.map(data, function(item) {
                            return {
                                label: item.Name,
                                value: item.Name
                            };
                        });
                        //call the filter here
                        response($.ui.autocomplete.filter(array, request.term));
                    }
                });
            },
            minLength: 0,
            focus: function (event, ui) {
                $("#txtSource" + obj).val(ui.item.label);
            }
        });
    }
</script>

<div class="col-md-3">
    <label class="" for="form-first-name">Source Type</label>
       <div class="clearfix">
           <select id="ddlSourceType" class="form-control" onchange="changeSourceType()">
                 <option value="0">Select</option>
                  <option value="1">Content</option>
                   <option value="2">Folder</option>
                   <option value="3">Taxonomy</option>
            </select>
      </div>
 </div>

 <div class="col-md-3">
       <label class="" for="form-first-name">Select Source</label>
          <div class="clearfix">
               <input type="text" id="txtSource1" class="form-control">
           </div>
 </div>

AutoComplete Link -: https://jqueryui.com/autocomplete/