Friday, 25 April 2014

How to Implement Calendar control in mvc 4 razor or Add Calender in TextBox in mvc4


Description:-

            In this Example we explain that how to add Calendar Control or Datepicker in a TextBox click in mvc 4 with Asp.Net using Jquery or Implement Calendar control in Asp.Net mvc.

This is the basic thing that we use calendar in every Forms or Razor View to Upload Date in Database. Here in this Example we Add the Calender in TextBox click to upload the Date of the File Upload by using Jquery and also implement the code in Controller class to save the data in database.

Here is a simple code to display Calender in MVC 4 Razor view

<script type="text/jscript">

 $(function () {
        $("#Upload_Date").datepicker(); // Here Upload_Date is the Id of the TextBox.
    });
    </script>

Here we also Describe to upload the File in Database to do this first we have to set this thing

@using (Html.BeginForm("Fileuploads", "FolderHierchy", FormMethod.Post, new { enctype = "multipart/form-data" }))

AND

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

Here "multipart/form-data" is used for when we upload the file in database and multiple="multiple" is used for to upload multiple file at a time.


 To handle this File Upload in controller you have to implement this code

[HttpPost]
        public ActionResult Fileuploads(Folder containers, IEnumerable<HttpPostedFileBase> files)
        {
          
            foreach (var file in files)
            {
                if (Convert.ToInt32(filesize) < 1073741824)//1GB Limit
                {
             
                     //your code
                }
             }
       }

Insert,Update,Delete in ModalPopup CRUD operation in ModalPopup

Read and Write in Text File in asp.Net Read and Write File in Asp.Net




FileUploads.cshtml:-

@model DMS.Models.Folder

@{
    ViewBag.Title = "FileUploads";
}
<style type="text/css">
    .ui-datepicker {
        font-size: 8pt !important;
    }
</style>
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery-1.7.1.js"></script>
<script src="~/Scripts/jquery-1.7.2.min.js">
</script>
<link type="text/css" href="~/Scripts/jquery-ui-1.8.19.custom.css" rel="stylesheet" />
<script src="~/Scripts/jquery-ui-1.8.19.custom.min.js"></script>
type="text/javascript"></script>
<script type="text/jscript">
    $(function () {
        $("#Upload_Date").datepicker();
    });


</script>
@Styles.Render("~/Content/Site1.css")
<h2>Create</h2>

@ViewData["message"]
@using (Html.BeginForm("Fileuploads", "FolderHierchy", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Folder_Master</legend>



        <div class="editor-label">
            @Html.LabelFor(model => model.Upload_Date)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Upload_Date)
            @Html.ValidationMessageFor(model => model.Upload_Date)
        </div>

        <div>
            <input type="file" name="files" id="file" multiple="multiple" />
        </div>

        <input type="submit" value="submit" name="submit" id="btnSubmit" />
        <div id="msg">&nbsp;</div>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}




FolderHierchyController:-

        [HttpPost]
        public ActionResult Fileuploads(Folder containers, IEnumerable<HttpPostedFileBase> files)
        {
         

            foreach (var file in files)
            {
                if (Convert.ToInt32(filesize) < 1073741824)//1GB Limit
                {
                    Folder_Master fm = new Folder_Master();
                    var fid = containers.Directorys.ID;

                    Folder_Master area = db.Folder_Master.SingleOrDefault(x => x.Folder_ID == fid);
                    GetFolder g = new GetFolder();
                    List<Folder> f = g.showSubGroup1((int)area.Sub_FolderId);
                    string path = null;
                    for (int i = f.Count - 1; i >= 0; i--)
                    {
                        path = path + f[i].Folder_Name + "/";
                    }

                    var pfnm = area.Folder_Name;

                    string folderPath = System.Web.HttpContext.Current.Server.MapPath("~/" + System.Configuration.ConfigurationManager.AppSettings["DMSfoldername"]) + "/" + path + pfnm;

                    var fileName = Path.GetFileName(file.FileName);

                    string fc = Path.GetExtension(file.FileName);
                    if (fc.Equals(".rar"))
                    {
                        ViewData["message"] = "you can not Upload .Rar File";
                    }

                    else if (fc.Equals(".zip"))
                    {
                        string paths = folderPath + "/" + file.FileName;
                        file.SaveAs(folderPath + "/" + file.FileName);
                        using (ZipFile zip = ZipFile.Read(paths))
                        {

                            zip.ExtractAll(folderPath, ExtractExistingFileAction.DoNotOverwrite);


                        }

                        var list = System.IO.Directory.GetFiles(folderPath, file.FileName);
                        foreach (var item in list)
                        {
                            System.IO.File.Delete(item);
                        }
                        string[] nm = file.FileName.Split('.');
                        string[] dir = Directory.GetFiles(folderPath + "/" + nm[0].ToString());
                        foreach (string item in dir)
                        {
                            string filename = Path.GetFileName(item);
                            string[] spfl = filename.Split('.');
                            File_List fl = new File_List();
                            fl.Folder_ID = (int)fid;
                            fl.File_Name = (spfl[0].ToString() + "_" + DateTime.Now.ToString("dd/mm/yy/hh/mm/ss") + "." + spfl[1].ToString()).Replace('/', ' ').Replace(" ", "");
                            file.SaveAs(folderPath + "/" + (spfl[0].ToString() + "_" + DateTime.Now.ToString("dd/mm/yy/hh/mm/ss") + "." + spfl[1].ToString()).Replace('/', ' ').Replace(" ", ""));

                            fl.Upload_Date = containers.Upload_Date;
                            fl.File_Date = DateTime.Now;
                            fl.DescriptionOF_File = containers.DescriptionOF_File;
                            fl.Version_No = 0;
                            fl.User_Id = 0;
                            db.File_List.Add(fl);
                            db.SaveChanges();
                            ViewData["message"] = "Uploaded Successfully";
                        }
                        Directory.Delete(folderPath + "/" + nm[0].ToString(), true);
                    }
                    else
                    {
                        file.SaveAs(folderPath + "/" + fileName);
                        string filename = Path.GetFileName(folderPath + "/" +fileName);
                        string[] spfl = filename.Split('.');
                        File_List fl = new File_List();
                        fl.Folder_ID = (int)fid;
                        fl.File_Name = (spfl[0].ToString() + "_" + DateTime.Now.ToString("dd/mm/yy/hh/mm/ss") + "." + spfl[1].ToString()).Replace('/', ' ').Replace(" ", "");
                        file.SaveAs(folderPath + "/" + (spfl[0].ToString() + "_" + DateTime.Now.ToString("dd/mm/yy/hh/mm/ss") + "." + spfl[1].ToString()).Replace('/', ' ').Replace(" ", ""));

                        fl.Upload_Date = containers.Upload_Date;
                        fl.File_Date = DateTime.Now;
                        fl.DescriptionOF_File = containers.DescriptionOF_File;
                        fl.Version_No = 0;
                        fl.User_Id = 0;
                        db.File_List.Add(fl);
                        db.SaveChanges();
                        ViewData["message"] = "Uploaded Successfully";
                    }
                }
                else
                {
                    ViewData["message"] = "File Size Does dont Exceed 20 MB.";
                }

            }
            //Save model

            filldirectory();
            return View("Fileuploads");
        }

0 comments:

Post a Comment