Dear *.*
I am new in developing project in MVC, the project i make is File Management System, which can store the File Informatin as (Master Table) and Image file(s) attached on Detail Entry (Detail Table),
as i clicked the Create button, i won't save on my tables.
pls help me.
heres the code
DocInfo.cs
using Microsoft.Extensions.Primitives;
using Microsoft.Identity.Client;
using Microsoft.VisualBasic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Numerics;
namespace FilMS.Models
{
public class DocInfo
{
public DocInfo() { }
public int Id { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM-dd-yyyy}" ,ApplyFormatInEditMode = true)]
public DateTime Received { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM-dd-yyyy}", ApplyFormatInEditMode = true)]
public DateTime Date { get; set; }
public string Origin { get; set; }
[ForeignKey("DocType")]
public int DocType_Id { get; set; }
public virtual DocType DocType { get; set; }
public string Particulars { get; set; }
public string ActionNeeded { get; set; }
public TimeOnly ActionTime { get; set; }
public string Receiver { get; set; }
public string ResponPerson { get; set; }
public string ContactNo { get; set; }
public string EmailAdd { get; set; }
[ForeignKey("StockRoom")]
public int StockRoom_Id { get; set; }
public virtual StockRoom StockRoom { get; set; }
public string FileNo { get; set; }
public string RackNo { get; set; }
public string? CreatedBy { get; set; } = string.Empty;
public string? UpdatedBy { get; set; } = string.Empty;
public DateTime? Modified { get; set; }
public virtual List<DocDetail> DocDetails { get; set; } = new List<DocDetail>();
}
}
DocDetail.cs
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace FilMS.Models
{
public class DocDetail
{
public DocDetail()
{
}
public int Id { get; set; }
[ForeignKey("DocInfo")]
public int DocInfo_Id { get; set; }
public virtual DocInfo DocInfo { get; set; }
public string? DocName { get; set; } = string.Empty;
public string FileUrl { get; set; }
[Display(Name = "Document Image")]
[NotMapped]
public IFormFile DocImage { get; set; }
}
}
DocInfoController.cs
using FilMS.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using System.Diagnostics.Tracing;
namespace FilMS.Controllers
{
public class DocInfoController : Controller
{
private readonly FilMSDbContext _context;
private readonly IWebHostEnvironment _environment;
public DocInfoController(FilMSDbContext context, IWebHostEnvironment environment)
{
this._context = context;
this._environment = environment;
}
public IActionResult Index()
{
var docInfo = _context.DocInfos.OrderBy(d => d.Received).ToList();
foreach (var obj in docInfo)
{
obj.DocType = _context.DocTypes.FirstOrDefault(d => d.Id == obj.DocType_Id);
}
foreach (var obj in docInfo)
{
obj.StockRoom = _context.StockRooms.FirstOrDefault(s => s.Id == obj.StockRoom_Id);
}
return View(docInfo);
}
[HttpGet]
public IActionResult Add()
{
DocInfo docInfo = new DocInfo();
docInfo.DocDetails.Add(new DocDetail() { Id = 1 });
PopulateDocType();
PopulateStockRoom();
return View(docInfo);
}
[HttpPost]
public IActionResult Add(DocInfo docInfo)
{
_context.SaveChanges();
return RedirectToAction("Index");
}
private string GetUploadedFileName(DocInfo docInfo)
{
string uniqueFileName = null;
return uniqueFileName;
}
[HttpGet]
public void PopulateDocType()
{
IEnumerable<SelectListItem> GetDocType =
_context.DocTypes.OrderBy(d => d.Type).Select(d => new SelectListItem
{
Text = d.Type,
Value = d.Id.ToString()
});
ViewBag.DocTypeList = GetDocType;
}
[HttpGet]
public void PopulateStockRoom()
{
IEnumerable<SelectListItem> GetStockRoom =
_context.StockRooms.OrderBy(s => s.RoomName).Select(s => new SelectListItem
{
Text = s.RoomName,
Value = s.Id.ToString()
});
ViewBag.StockRoomList = GetStockRoom;
}
}
}
Add.cshtml
@model FilMS.Models.DocInfo
@{
ViewData["Title"] = "New Document";
}
<h1>New Document</h1>
<hr />
<div class="row">
<div class="col-md-12">
<form asp-action="Add" id="DocInfoEntryForm" enctype="multipart/form-data">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="col-md-12">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label asp-for="Origin" class="control-label">Origin</label>
<input asp-for="Origin" class="form-control" />
<span asp-validation-for="Origin" class="text-danger"></span>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label asp-for="Received" class="control-label">Date Received</label>
<input asp-for="Received" class="form-control" asp-format="{0:MM/dd/yyyy}" />
<span asp-validation-for="Received" class="text-danger"></span>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label asp-for="Date" class="control-label">Date</label>
<input asp-for="Date" class="form-control" asp-format="{0:MM/dd/yyyy}" />
<span asp-validation-for="Date" class="text-danger"></span>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label asp-for="DocType_Id" class="control-label">Type of Document</label>
<select asp-for="DocType_Id" asp-items="@ViewBag.DocTypeList" class="form-select">
<option disabled selected>-- Document Type --</option>
</select>
<span asp-validation-for="DocType_Id" class="text-danger"></span>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label asp-for="Particulars" class="control-label"></label>
<textarea asp-for="Particulars" class="form-control"></textarea>
<span asp-validation-for="Particulars" class="text-danger"></span>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label asp-for="ActionNeeded" class="control-label">Action Needed</label>
<input asp-for="ActionNeeded" class="form-control" />
<span asp-validation-for="ActionNeeded" class="text-danger"></span>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label asp-for="ActionTime" class="control-label"></label>
<input asp-for="ActionTime" class="form-control" />
<span asp-validation-for="ActionTime" class="text-danger"></span>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label asp-for="Receiver" class="control-label">Receiver</label>
<input asp-for="Receiver" class="form-control" />
<span asp-validation-for="Receiver" class="text-danger"></span>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label asp-for="ResponPerson" class="control-label">Responsible Person</label>
<input asp-for="ResponPerson" class="form-control" />
<span asp-validation-for="ResponPerson" class="text-danger"></span>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label asp-for="ContactNo" class="control-label">Contact No.</label>
<input asp-for="ContactNo" class="form-control" />
<span asp-validation-for="ContactNo" class="text-danger"></span>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label asp-for="EmailAdd" class="control-label">Email</label>
<input asp-for="EmailAdd" class="form-control" />
<span asp-validation-for="EmailAdd" class="text-danger"></span>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label asp-for="StockRoom_Id" class="control-label">File Room</label>
<select asp-for="StockRoom_Id" asp-items="@ViewBag.StockRoomList" class="form-select">
<option disabled selected>-- File Room --</option>
</select>
<span asp-validation-for="StockRoom_Id" class="text-danger"></span>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label asp-for="FileNo" class="control-label">File No</label>
<input asp-for="FileNo" class="form-control" />
<span asp-validation-for="FileNo" class="text-danger"></span>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label asp-for="RackNo" class="control-label">Rack No</label>
<input asp-for="RackNo" class="form-control" />
<span asp-validation-for="RackNo" class="text-danger"></span>
</div>
</div>
</div>
</div>
<hr />
<div class="row">
<label class="form-label">Attach Scanned Document(s)</label>
</div>
<div class="col-md-12">
<table id="DocTable" class="table">
<thead>
<tr>
<th>Image File</th>
<th>Description of Attached Image File</th>
<th style="width:75px;">
<button id='btnaddImgFile' type="button" class="btn btn-sm btn-secondary visible"
onclick="AddItem(this)">
Add
</button>
</th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.DocDetails.Count; i++)
{
<tr>
<td>
@* <input asp-for="@Model.DocDetails[i].ImageFile" class="form-control ImageFile" /> *@
<input asp-for="@Model.DocDetails[i].DocImage" class="form-control ImageFile" />
</td>
<td>
<input asp-for="@Model.DocDetails[i].DocName" class="form-control" />
</td>
<td style="width:350px;">
<button id="btnCapture" type="submit" class="btn btn-sm btn-outline-success">Capture Image</button>
<button id="btnImportImage" type="submit" class="btn btn-sm btn-outline-primary">Import Image</button>
<button id='btnremove-@i' type="button" class="btn btn-sm btn-danger visible"
onclick="DeleteItem(this)">
Delete
</button>
</td>
</tr>
}
</tbody>
</table>
</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");}
}