Thursday, 21 July 2016

Pass and Return different class in generic class for Reflection

No comments
Generic (Reflection) Method

 public System.Collections.Generic.List<T> GetValues<T>(object t) where T : class
        {          
            System.Collections.Generic.List<T> lstUserAppconfig = new System.Collections.Generic.List<T>();
            foreach (PropertyInfo info in t.GetType().GetProperties())
            {
                if (Enum.IsDefined(typeof(Constant.Menus), info.Name))//check if value is in enum
                {
                    if (!string.IsNullOrEmpty((string)info.GetValue(t, null)))
                    {
                        UserAppconfig userAppconfig = new UserAppconfig();
                        userAppconfig.appconfigid = (int)Enum.Parse(typeof(Constant.Menus), info.Name);
                        userAppconfig.userid = SessionFactory.Instance.CurrentUsers.userid;
                        userAppconfig.value = (string)info.GetValue(t, null);
                        lstUserAppconfig.Add((T)Convert.ChangeType(userAppconfig, typeof(T)));
                    }
                }
            }
            return lstUserAppconfig;

        }

Calling method

UserAppconfigModel userAppconfigModel=new UserAppconfigModel()

System.Collections.Generic.List<UserAppconfig> lstUserAppconfig=GetValues<UserAppconfig>(userAppconfigModel);

read more

Post JSON to MVC controller

No comments
JQUERY

var array=[];
$("[name='Sync']").each(function () {               
                var key = ($(this).is(':checked'))
                if (key == false) {
                    var value = ($(this).attr('data-id'))
                    array.push({ key: key, value: value });
                }
            });            
            $.ajax({
                url: '@Url.Action("ChildChannelProductDB", "Products"),
                data: JSON.stringify(array),
                type: "POST",
                success: function (data, textStatus, XMLHttpRequest) {
                    SetData(data);
                }
            });



Model

  public class ArrayResponseModel
    {
        public string key { get; set; }
        public string value { get; set; }
    }

Controller

var resolveRequest = HttpContext.Request;
            List<ArrayResponseModel> model = new List<ArrayResponseModel>();
            resolveRequest.InputStream.Seek(0, SeekOrigin.Begin);
            string jsonString = new StreamReader(resolveRequest.InputStream).ReadToEnd();
                 if (jsonString != null)
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                model = (List<ArrayResponseModel>)serializer.Deserialize(jsonString, typeof(List<ArrayResponseModel>));
            }

read more

Wednesday, 20 July 2016

Different approaches to make ajax call in MVC

No comments
JQUERY

$.ajax({
                    type: "GET",
                    url: "@Url.Action("GetZipcode", "Default")",
                    data: { postalcode: $(this).val() },
                    async: true,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (responseData) {
                        debugger
                        if (responseData != null && responseData != undefined && responseData != "") {
                            $('#city').val(responseData.city);
                            $('#stateid').val(responseData.stateid).trigger("chosen:updated");;
                        }
                        else {
                            $('#city').val('');
                            $('#stateid option').eq(0).attr('selected', 'selected');
                            $("#stateid").trigger("chosen:updated");
                        }
                    },
                    error: function (e) {
                        $('#city').val('');
                        $('#stateid option').eq(0).attr('selected', 'selected');
                        $("#stateid").trigger("chosen:updated");
                    }
                });


Controller

 public string GetZipcode(string postalcode)
        {
            var postalCodesRepository = uow.Repository<PostalCodesRepository>();
            var postalcodeData = postalCodesRepository.GetDataByZip(postalcode);
            return Newtonsoft.Json.JsonConvert.SerializeObject(postalcodeData);

        }

 public JsonResult GetZipcode()
        {
             var postalCodesRepository = uow.Repository<PostalCodesRepository>();
            var postalcodeData = postalCodesRepository.GetDataByZip(postalcode);
            return Json(postalcodeData , JsonRequestBehavior.AllowGet);
        }
read more

Sunday, 10 July 2016

Enable Sorting with Jquery DataTable

No comments
Table -

                              <table id="tableView" class="table tablesorter">
                                        <thead>
                                            <tr>
                                                <th class="textLeft">
                                                    email
                                                </th>
                                           </tr>
                                        </thead>
                                        <tbody>
                                            @foreach (var item in @Model.lstPatient)
                                            {
                                                 <tr>
                                                 <td>@item.email</td>
                                                 </tr>
                                             }
                                        </tbody>
                                </table>



JS


@section Scripts
        {
        <script type="text/javascript">

            $(document).ready(function () {
                var mSortingString = [];
                var disableSortingColumn = 4;
                mSortingString.push({ "bSortable": false, "aTargets": [disableSortingColumn] });


                $('#tableView').DataTable({
                    "paging": false,
                    "ordering": true,
                    "info": false,
                    "bFilter": false,
                    "bInfo": false,
                    "aaSorting": [[0, 'desc']]

                });
             });
</script>
 }
read more