Tuesday, July 9, 2019

MVC DateTime binding with incorrect date format

Set following culture and date format in Global.ASAX Application_BeginRequest method.
It will fix the issue

protected void Application_BeginRequest()
{
// Set your culture for example "en-GB"
    CultureInfo cInf = new CultureInfo("en-GB", false);   
 
    cInf.DateTimeFormat.DateSeparator = "/";
    cInf.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy"; //You may add your specific format

//You may add your specific format
    cInf.DateTimeFormat.LongDatePattern = "dd/MM/yyyy hh:mm:ss tt";

    System.Threading.Thread.CurrentThread.CurrentCulture = cInf;
    System.Threading.Thread.CurrentThread.CurrentUICulture = cInf;
}

Monday, June 24, 2019

Convert Javascript Object to form data

//Following function will return formdata for your javascript object
//It will process Array and File Data Types also.
//It will process Array and File Data Types also.


function GenerateFormData(obj, form, Namespace) {
    var ObjKeys = Object.keys(obj)
    var formData = form || new FormData();
    $.each(ObjKeys, function (index, key) {
        if (obj[key] instanceof Array) {
            for (var i = 0; i < obj[key].length; i++) {
                var ObjPrefix = "";
                if (Namespace)
                {
                    ObjPrefix = Namespace + "." + key + "[" + i + "]";
                }
                else {
                    ObjPrefix =  key + "[" + i + "]";
                }
                if (obj[key][i] instanceof File) {
                    formData.append(ObjPrefix, obj[key][i]);
                }
                else if (obj[key][i] instanceof Object) {
                    formData = GenerateFormData(obj[key][i], formData, ObjPrefix);
                }
                else {
                    formData.append(ObjPrefix, obj[key][i]);
                }
 
            }
 
        }
        else {
            var ObjPrefix = "";
            if (Namespace) {
                ObjPrefix = Namespace + "." + key;
            }
            else {
                ObjPrefix = key;
 
            }
 
            if (obj[key] instanceof File) {
                formData.append(ObjPrefix, obj[key]);
            }
            else if (obj[key] instanceof Object) {
                formData = GenerateFormData(obj[key], formData, ObjPrefix);
            }
            else {
                formData.append(ObjPrefix, obj[key]);
            }
        }
    });
 
    return formData;
}

var obj={ID:10,Name:"Udayakumar",Age:18,Subject:["Tamil","English","Maths","Science"]};
var UmDataCollection=GenerateFormData(obj,null,"model");

Thursday, June 6, 2019

Upload Multiple files asynchronously in Jquery and MVC

<input type="file" id="file" name="file" />

<input type="button" class="button" value="Upload" id="btnUpload"> </div> <script type="text/javascript"> $(document).ready(function() { $("#btnUpload").click(function() { var formdata = new FormData(); var files = $('#file')[0].files[0]; for(var i=0;files.length;i++) { formdata.append('files['+i+"]", files[i]); } $.ajax({ url: 'UploadFiles', type: 'post', data: formdata, contentType: false, processData: false, success: function(result){ if(result){ alert('file uploaded successfully'); } else{ alert('file upload failure'); } }, }); }); }); </script> C# --------------------------------------------- public ActionResult UploadFiles(List<HttpPostedFileBase> files) { var fileList=files; return json(true) }

How to get uploaded files using fileinput.js ?


By using following code you can get uploaded files

var fileStack=$('#fileUploadControl).fileinput('getFileStack');