+
+
+
+
+
-
-
-
-
- @{
- foreach (var item in Model.ExcelItems)
- {
-
-
-
-
- }
- }
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ | Vrstica |
+ Polje |
+ Vrednost |
+ Napaka |
+
+
+
+
+
+
+
+
+
+
-
-
-
+@Html.AntiForgeryToken()
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
diff --git a/EveryThing/Pages/Projects/CreatePartItemImportExcel.cshtml.cs b/EveryThing/Pages/Projects/CreatePartItemImportExcel.cshtml.cs
index ee15c53..a908eb2 100644
--- a/EveryThing/Pages/Projects/CreatePartItemImportExcel.cshtml.cs
+++ b/EveryThing/Pages/Projects/CreatePartItemImportExcel.cshtml.cs
@@ -48,31 +48,48 @@ namespace EveryThing.Pages.Projects
public string SelectedItems { get; set; }
- public IActionResult OnGet(int idProject, int idProjectPart, string fileName)
+ public IActionResult OnGet(int idProject, int idProjectPart)
{
- var user = _userManager.GetUserAsync(User).Result;
-
IdProject = idProject;
IdProjectPart = idProjectPart;
- FileName = fileName;
-
- var tmpList = typeof(Models.Project.ProjectPartItem).GetProperties()
- .Where(x => x.GetCustomAttributes(true).Length > 0 && x.GetCustomAttributes(true).Any(y => y.GetType() == typeof(System.ComponentModel.DataAnnotations.DisplayAttribute)))
- .Select(x => new
- {
- Name = x.Name,
- Display = ((System.ComponentModel.DataAnnotations.DisplayAttribute)x.GetCustomAttributes(true).First(y => y.GetType() == typeof(System.ComponentModel.DataAnnotations.DisplayAttribute))).Name
- }).ToList();
- tmpList.Insert(0, new { Name = "", Display = "Ni izbrano" });
-
- ViewData["ProjectPartItems"] = new SelectList(tmpList, "Name", "Display");
-
ExcelItems = new List
();
- var path = Path.Combine(_hostingEnvironment.WebRootPath, "Uploads", "TempExcelImport", fileName);
- var xlWorkbook = new XLWorkbook(path);
-
- //ONLY FIRST LIST
+ return Page();
+ }
+
+ public async Task OnPostUpload(int idProject, int idProjectPart, List postedFiles)
+ {
+ if (postedFiles == null
+ || postedFiles.Count != 1)
+ {
+ return new JsonResult(new { successful = false, error = "Izberite eno datoteko." });
+ }
+ var path = Path.Combine(_hostingEnvironment.WebRootPath, "Uploads", "TempExcelImport");
+ if (!Directory.Exists(path))
+ {
+ Directory.CreateDirectory(path);
+ }
+ else
+ {
+ //Pocistimo mapo
+ foreach (var fileInfo in new DirectoryInfo(path).GetFiles("*.*"))
+ {
+ fileInfo.Delete();
+ }
+ }
+
+ var postedFile = postedFiles[0];
+
+ var fileName = postedFile.FileName;
+ await using (var stream = new FileStream(Path.Combine(path, fileName), FileMode.Create))
+ {
+ await postedFile.CopyToAsync(stream);
+ }
+
+ // Read excel headers
+ var excelItems = new List();
+ var filePath = Path.Combine(path, fileName);
+ var xlWorkbook = new XLWorkbook(filePath);
var worksheet = xlWorkbook.Worksheet(1);
int i = 1;
@@ -82,26 +99,36 @@ namespace EveryThing.Pages.Projects
int j = 1;
while (!row.Cell(j).IsEmpty())
{
- var cellData = row.Cell(j).Value;
-
- ExcelItems.Add(new ExcelItem
+ excelItems.Add(new ExcelItem
{
CellIndex = j,
- Name = cellData.ToString(),
+ Name = row.Cell(j).Value.ToString(),
});
-
j++;
}
}
- return Page();
+ // Build mapping options
+ var mappingOptions = typeof(Models.Project.ProjectPartItem).GetProperties()
+ .Where(x => x.GetCustomAttributes(true).Length > 0 && x.GetCustomAttributes(true).Any(y => y.GetType() == typeof(System.ComponentModel.DataAnnotations.DisplayAttribute)))
+ .Select(x => new
+ {
+ name = x.Name,
+ display = ((System.ComponentModel.DataAnnotations.DisplayAttribute)x.GetCustomAttributes(true).First(y => y.GetType() == typeof(System.ComponentModel.DataAnnotations.DisplayAttribute))).Name
+ }).ToList();
+
+ return new JsonResult(new { successful = true, fileName, excelItems, mappingOptions });
}
- public async Task OnPostAsync(string selectedItems)
+ public async Task OnPostImport(string selectedItems, string fileName, int idProjectPart, int idProject)
{
- if (selectedItems == "")
+ FileName = fileName;
+ IdProjectPart = idProjectPart;
+ IdProject = idProject;
+
+ if (string.IsNullOrEmpty(selectedItems))
{
- return Page(); //TODO Error
+ return new JsonResult(new { successful = false, error = "Izberite vsaj eno polje." });
}
var user = _userManager.GetUserAsync(User).Result;
@@ -128,6 +155,73 @@ namespace EveryThing.Pages.Projects
//ONLY FIRST LIST
var worksheet = xlWorkbook.Worksheet(1);
+ // Build display name lookup for properties
+ var propertyDisplayNames = typeof(Models.Project.ProjectPartItem).GetProperties()
+ .Where(x => x.GetCustomAttributes(true).Any(y => y is System.ComponentModel.DataAnnotations.DisplayAttribute))
+ .ToDictionary(
+ x => x.Name,
+ x => ((System.ComponentModel.DataAnnotations.DisplayAttribute)x.GetCustomAttributes(true).First(y => y is System.ComponentModel.DataAnnotations.DisplayAttribute)).Name
+ );
+
+ // Validation pass - check all rows before importing
+ var errors = new List