Hi Friends,
I was tries to save data to db with image path and save image to directory folder wwwroot path. All the code looks ok no error appers but I don't know why the data not saved to db when button submited click.
please help me.
using System.Security.Cryptography.X509Certificates;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace lma_apps_v._01.Models
{
[Table("tbl_driver")]
public partial class TblDriver
{
[Key]
[Column("no_id")]
public int NoId { get; set; }
[Column("area")]
[StringLength(150)]
[Unicode(false)]
public string? Area { get; set; }
[Column("lokasi")]
[StringLength(150)]
[Unicode(false)]
public string? Lokasi { get; set; }
[Column("proyek")]
[StringLength(150)]
[Unicode(false)]
public string? Proyek { get; set; }
[Column("nama_lengkap")]
[StringLength(150)]
[Unicode(false)]
public string? NamaLengkap { get; set; }
[Column("nik_karyawan")]
[StringLength(150)]
[Unicode(false)]
public string? NikKaryawan { get; set; }
[Column("tgl_masuk_kerja", TypeName = "date")]
public DateTime? TglMasukKerja { get; set; }
[Column("tgl_lahir", TypeName = "date")]
public DateTime? TglLahir { get; set; }
[Column("nik_ktp")]
[StringLength(150)]
[Unicode(false)]
public string? NikKtp { get; set; }
[Column("no_kk")]
[StringLength(150)]
[Unicode(false)]
public string? NoKk { get; set; }
[Column("alamat_lengkap")]
[StringLength(250)]
[Unicode(false)]
public string? AlamatLengkap { get; set; }
[Column("no_sim")]
[StringLength(150)]
[Unicode(false)]
public string? NoSim { get; set; }
[Column("jenis_sim")]
[StringLength(150)]
[Unicode(false)]
public string? JenisSim { get; set; }
[Column("tgl_berlaku_sim", TypeName = "date")]
public DateTime? TglBerlakuSim { get; set; }
[Column("no_rekening")]
[StringLength(150)]
[Unicode(false)]
public string? NoRekening { get; set; }
[Column("nama_bank")]
[StringLength(150)]
[Unicode(false)]
public string? NamaBank { get; set; }
[Column("nama_direkening")]
[StringLength(150)]
[Unicode(false)]
public string? NamaDirekening { get; set; }
[Column("keterangan")]
[StringLength(250)]
[Unicode(false)]
public string? Keterangan { get; set; }
[Column("img_url")]
[Display(Name="URL Path Foto Driver")]
public string? img_url { get; set; }
}
}
@model lma_apps_v._01.Models.TblDriver
@{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>TblDriver</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create" method="post" enctype="multipart/form-data">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Area" class="control-label"></label>
<input asp-for="Area" class="form-control" />
<span asp-validation-for="Area" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="img_url" class="control-label"></label>
<input type="file" name="file" class="form-control" />
<span asp-validation-for="img_url" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Lokasi" class="control-label"></label>
<input asp-for="Lokasi" class="form-control" />
<span asp-validation-for="Lokasi" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Proyek" class="control-label"></label>
<input asp-for="Proyek" class="form-control" />
<span asp-validation-for="Proyek" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="NamaLengkap" class="control-label"></label>
<input asp-for="NamaLengkap" class="form-control" />
<span asp-validation-for="NamaLengkap" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="NikKaryawan" class="control-label"></label>
<input asp-for="NikKaryawan" class="form-control" />
<span asp-validation-for="NikKaryawan" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="TglMasukKerja" class="control-label"></label>
<input asp-for="TglMasukKerja" class="form-control" />
<span asp-validation-for="TglMasukKerja" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="TglLahir" class="control-label"></label>
<input asp-for="TglLahir" class="form-control" />
<span asp-validation-for="TglLahir" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="NikKtp" class="control-label"></label>
<input asp-for="NikKtp" class="form-control" />
<span asp-validation-for="NikKtp" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="NoKk" class="control-label"></label>
<input asp-for="NoKk" class="form-control" />
<span asp-validation-for="NoKk" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="AlamatLengkap" class="control-label"></label>
<input asp-for="AlamatLengkap" class="form-control" />
<span asp-validation-for="AlamatLengkap" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="NoSim" class="control-label"></label>
<input asp-for="NoSim" class="form-control" />
<span asp-validation-for="NoSim" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="JenisSim" class="control-label"></label>
<input asp-for="JenisSim" class="form-control" />
<span asp-validation-for="JenisSim" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="TglBerlakuSim" class="control-label"></label>
<input asp-for="TglBerlakuSim" class="form-control" />
<span asp-validation-for="TglBerlakuSim" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="NoRekening" class="control-label"></label>
<input asp-for="NoRekening" class="form-control" />
<span asp-validation-for="NoRekening" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="NamaBank" class="control-label"></label>
<input asp-for="NamaBank" class="form-control" />
<span asp-validation-for="NamaBank" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="NamaDirekening" class="control-label"></label>
<input asp-for="NamaDirekening" class="form-control" />
<span asp-validation-for="NamaDirekening" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Keterangan" class="control-label"></label>
<input asp-for="Keterangan" class="form-control" />
<span asp-validation-for="Keterangan" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{
await Html.RenderPartialAsync("_ValidationScriptsPartial");
}
}
// POST: Driver/Create
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("NoId,Area,Lokasi,Proyek,NamaLengkap,NikKaryawan,TglMasukKerja,TglLahir,NikKtp,NoKk,AlamatLengkap,NoSim,JenisSim,TglBerlakuSim,NoRekening,NamaBank,NamaDirekening,Keterangan,img_url")] TblDriver driver, IFormFile file)
{
if (ModelState.IsValid)
{
try
{
// Validasi tipe berkas dan ekstensi, serta ukuran maksimal
if (file != null)
{
if (!IsImage(file))
{
ModelState.AddModelError("img_url", "File harus berupa gambar (jpeg, jpg, png, atau gif).");
}
else if (!IsAllowedExtension(file.FileName))
{
ModelState.AddModelError("img_url", "Ekstensi file tidak diizinkan. Harap unggah file dengan ekstensi .jpeg, .jpg, .png, atau .gif.");
}
else if (file.Length > 2 * 1024 * 1024) // 2MB
{
ModelState.AddModelError("img_url", "Ukuran file gambar tidak boleh melebihi 2 MB.");
}
else
{
await SaveImgUrl(file, driver);
}
}
_context.Add(driver);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
catch (Exception ex)
{
// Tangani pengecualian, log, dan berikan umpan balik yang sesuai kepada pengguna.
ModelState.AddModelError(string.Empty, $"Error: {ex.Message}");
}
}
return View(driver);
}
//SaveImgUrl method untuk save file gambar ke directory wwwwroot aplikasi
private async Task SaveImgUrl(IFormFile file, TblDriver tblDriver)
{
if (file != null && file.Length > 0)
{
string folder = "uploads/image/driversandoperators/" + Guid.NewGuid().ToString() + "-" + file.FileName;
using (var stream = new FileStream("wwwroot" + folder, FileMode.Create))
{
await file.CopyToAsync(stream);
}
tblDriver.img_url = folder;
}
}
//IsImage method untuk Validasi tipe berkas gambar
private bool IsImage(IFormFile file)
{
// Validasi tipe berkas gambar
return file.ContentType.StartsWith("image/");
}
//IsAllowedExtension method untuk Validasi ekstensi file yang diizinkan
private bool IsAllowedExtension(string fileName)
{
// Validasi ekstensi file yang diizinkan
string[] allowedExtensions = { ".jpeg", ".jpg", ".png", ".gif" };
string fileExtension = System.IO.Path.GetExtension(fileName).ToLowerInvariant();
return allowedExtensions.Contains(fileExtension);
}