In this article i will show you how you can connect and fetch data from my sql database in your asp.net core 6 application.
In this article i will show you how you can connect and fetch data from my sql database in your asp.net core 6 application.
In this article i will show you how you can download the uploaded file in wwwroot folder in your asp.net core 6 / mvc application.
In my previous article i have shown you how you can upload file in asp.net core 6 using c# and other article like upload file and rename it before uploading in wwwroot folder in asp.net core .
Other related are as follows: Asp.Net Core 6 : Rename / Change File or Image Name and Upload in wwwroot Using C#.Net, ASP.NET Core 6 : Ajax Upload File( Without Page Refresh) To wwwroot Folder in Using C#.Net , jQuery, Asp.Net Core 6 : Upload File in User Created Folder (Folder Outside wwwroot) Using C#, File Upload in wwwroot Folder Using Input File Control(HTML) in Asp.Net Core 6/MVC Using C#
Now for this article we will create a new asp.net core application and in wwwroot folder we will add a static file in it. This file we will download on button click on our asp.net core application.
Now on controller end we will create a httpget method.
[HttpGet] public IActionResult Index() {
return View(); } |
After this we will create a view and and add link or button control. On click on this control we will download the file.
@{
ViewData["Title"] = "Page"; } <div class="text-center"> <h1>Click Link to download file</h1><br /><br /> <a href="/Home/Index/" title="Download file"><b>DOWNLOAD</b></a> </div> |
After this we will write code to download the file. For this we will create a HttpPost method in controller.
public IActionResult DownloadFile() { var memory = DownloadSinghFile("TextFile.txt", "wwwwroot"); return File(memory.ToArray(), "text/plain", "TextFile.txt"); } private MemoryStream DownloadSinghFile(string filename, string uploadPath) { var path = Path.Combine(Directory.GetCurrentDirectory(),
uploadPath, filename); var memory = new
MemoryStream(); if (System.IO.File.Exists(path)) { var net = new System.Net.WebClient(); var data = net.DownloadData(path); var content = new System.IO.MemoryStream(data); memory = content; } memory.Position = 0; return memory; } |
In above code i have passed the file detail and return type of the function is MonoryStream. After that i have user File return type to convert the memorystream in file. In this file return type you must pass the type and file name. Here is the list of file type which you can refer.
{".txt", "text/plain"}, {".pdf", "application/pdf"}, {".doc", "application/vnd.ms-word"}, {".docx", "application/vnd.ms-word"}, {".xls", "application/vnd.ms-excel"}, {".xlsx", "application/vnd.ms-excel"}, {".png", "image/png"}, {".jpg", "image/jpeg"}, {".jpeg", "image/jpeg"}, {".gif", "image/gif"}, {".csv", "text/csv"}, {".zip", "application/zip"} |
Now we have done run the code to check the output.
Now click on download link to download the file.
Download
In this article i will show you how you can bind the model class property of list type to control present in table and access the added value at controller end in asp.net core using C#.net.
Other Related Articles: Asp.Net Core 6: Bind ListBox(ListBoxFor) By EntityFrameWork Using C#.Net | Sql Server, Asp.Net Core 6: Add(Create) Dynamic Row With TextBox/TextBoxFor To Table and Get Control Value at Controller End, jQuery, C#.Net, How To Read / Access Email Setting In Appsettings.json In Asp.Net Core 6 in Controller Using C#.Net, Search And Display Data In Table/Tabular Format in Asp.net Core 6/MVC using C# (Ms Sql Server), Display Data In Table/Tabular Format In Asp Net Core 6/MVC Using C#.Net (Ms Sql Database), Asp.net Core 6: Ajax Login Form Using jQuery, C# Validate From Ms Sql Server Database.
Now for this article first we will create a new asp.net core and create a mode class file.
public class EmployeeModel
{ public List<EmployeeDetail> EmployeeList { get; set; }
}
public class EmployeeDetail
{ public int Id { get; set; } public string EmployeeName { get; set; } public string Location { get; set; } public int Salery
{ get; set; } } |
After this model we will add the HttpGet method in the controller. In this method we will create the object of model class, and pass it to the view to bind it to control.
[HttpGet] public IActionResult Index() { EmployeeModel employeeModel = new EmployeeModel(); employeeModel.EmployeeList = new List<EmployeeDetail>(); //Here
i have adding donw dummy value as id //In
this we will create 5 rows for (int i = 0;
i < 5; i++) { employeeModel.EmployeeList.Add(new EmployeeDetail { Id = i }); }
return View(employeeModel); } |
In above code i have added 3 rows to the list. Ones be bind we will get 3 rows table and we will provide other detail and try to access it. This is just for generating no of rows to the list. You can use your own model with data in list to bind. After this we will create .cshtml view file and add the below code.
@model Project.Models.EmployeeModel @{
ViewData["Title"] = "Page"; } @using (Html.BeginForm("Index", "Home",
FormMethod.Post, new {
@enctype = "multipart/form-data" })) {
<div class="text-center"> <table class="table table-bordered"> <thead> <tr> <td>Id</td> <td>Name</td> <td>Location</td> <td>Salery</td> </tr> </thead> <tbody> @{ int count = 0;} @foreach (var item in
Model.EmployeeList) { <tr> <td>@(item.Id+1)</td> <td>@Html.TextBoxFor(m
=> Model.EmployeeList[count].EmployeeName, new { @class = "form-control" })</td> <td>@Html.TextBoxFor(m => Model.EmployeeList[count].Location, new { @class = "form-control" })</td> <td>@Html.TextBoxFor(m => Model.EmployeeList[count].Salery, new { @class = "form-control" })</td> </tr> count = count + 1; } </tbody> </table> <input type="submit" value="Submit" />
</div> } |
In above code i have used foreach loop to generate the rows. In this i have taken the index variable named as count. I am increasing each time in look. In this I have used model list by assigning the index of each and every control. Now we have done run the code to check the output.
In this article i will show you how you can add no of days to the to user entered date using jquery. So for this first we will add the jquery library reference into the project and add the date in dd/mm/yyyy format into the textbox.
Here is html of the entry form.
<div class="text-center"> Enter Date: <input type="text" placeholder="dd/mm/yyyy" id="txtuserinoutdate" /><br /> No of Days: <input type="text" placeholder="noofdays" id="txtnoofdays" /><br /> Calculated Date: <input type="text" placeholder="dd/mm/yyyy" id="txtcalculateddate" /> <br /> <br /> <input type="button" value="Calculate" onclick="javascript: CalculateDate();" /> </div> |
Now we will add the jQuery file reference and add the add the below javascript function code.
var CalculateDate = function () { var userdate = new Date($("#txtuserinoutdate").val()); var noofdays = $("#txtnoofdays").val(); var calculateddate = new Date(addDays(getFormattedString(userdate), noofdays)); $("#txtcalculateddate").val(getFormattedString(calculateddate)); } var getFormattedString = function (d) { return ('0' + d.getDate()).toString().slice(-2) + "/" + ('0' + (d.getMonth() +
1)).toString().slice(-2) + "/" + d.getFullYear(); } var addDays = function (input_date, days) { var currentDate = new Date(input_date); if (days != 0) {
currentDate.setDate(currentDate.getDate() + parseInt(days)); } return currentDate; } |
In above code i have used three javascript function. In this first function is called on click event of the button control and on other hand getformatedString is used for reformatting the user entered date. and addDays function is used for adding the no of days to the user provided date in dd/mm/yyyy format. Now we have done run the page and check the output.
Now add the date and no days which you want to add and click on Calculate.