I want to insert null value for LastLoggedDate DATETIME during registration so that when the person first logs into the system,it will check the LastLoggedDate field in the database is null.If it is null it should redirect to change password page so that the person can change his password and also update the LastLoggedDate with the current date else the person should remain on the index page.
CREATE TABLE UserDetails
(
UserID VARCHAR(50),
Password VARCHAR(50),
Name VARCHAR(50),
MobNumber BIGINT,
EmailID VARCHAR(50),
EmpID VARCHAR(50),
RoleID VARCHAR(50),
DesignID VARCHAR(50),
LastLoggedDate DATETIME,
CreatedBy VARCHAR(50)
)
Model
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace EF_Core_7_MVC.Models
{
[Table("UserDetails")]
public class UserDetail
{
[Key]
public string? UserID { get; set; }
public string? Password { get; set; }
public string? Name { get; set; }
public Int64 MobNumber { get; set; }
public string? EmailID { get; set; }
public string? EmpID { get; set; }
public string? RoleID { get; set; }
public string? DesignID { get; set; }
public DateTime? LastLoggedDate { get; set; }
public string? CreatedBy { get; set; }
}
}
Interface
using EF_Core_7_MVC.Models;
namespace EF_Core_7_MVC.Abstract
{
public interface IUserDetailManagement
{
string InsertUserDatail(UserDetail usr);
}
}
Repository Class
using EF_Core_7_MVC.Abstract;
using EF_Core_7_MVC.Models;
namespace EF_Core_7_MVC.Repository
{
public class UserDetailDataManagement : IUserDetailManagement
{
UserDetailDbContext _context = new UserDetailDbContext();
public string InsertUserDatail(UserDetail usr)
{
string msg = string.Empty;
try
{
usr.CreatedBy = usr.Name;
usr.LastLoggedDate = DateTime.Now;
_context.UserDetails.Add(usr);
_context.SaveChanges();
msg = "success";
}
catch (Exception ex)
{
msg = "failed";
}
return msg;
}
}
}
DBContext
using EF_Core_7_MVC.Models;
using Microsoft.EntityFrameworkCore;
namespace EF_Core_7_MVC
{
public class UserDetailDbContext : DbContext
{
public DbSet<UserDetail> UserDetails { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Server=.\\SQL2022;Initial Catalog=AjaxSamples;uid=sa;pwd=pass@123;Trusted_Connection=True;TrustServerCertificate=true;");
}
base.OnConfiguring(optionsBuilder);
}
}
}
Controller
using EF_Core_7_MVC.Models;
using Microsoft.AspNetCore.Mvc;
namespace EF_Core_7_MVC.Controllers
{
public class HomeController : Controller
{
EF_Core_7_MVC.Repository.UserDetailDataManagement objBL = new EF_Core_7_MVC.Repository.UserDetailDataManagement();
public ActionResult Index()
{
return View();
}
[HttpGet]
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(UserDetail usr)
{
if (ModelState.IsValid)
{
string msg = objBL.InsertUserDatail(usr);
if (msg == "success")
{
return RedirectToAction("Index");
}
else
{
ViewBag.ErrorInfo = "Data not saved, try again latter";
}
}
return RedirectToAction("Index");
}
}
}
@model EF_Core_7_MVC.Models.UserDetail
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
</head>
<body class="container">
<h4>UserDetail</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="UserID" class="control-label"></label>
<input asp-for="UserID" class="form-control" />
<span asp-validation-for="UserID" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Password" class="control-label"></label>
<input asp-for="Password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="MobNumber" class="control-label"></label>
<input asp-for="MobNumber" class="form-control" />
<span asp-validation-for="MobNumber" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="EmailID" class="control-label"></label>
<input asp-for="EmailID" class="form-control" />
<span asp-validation-for="EmailID" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="EmpID" class="control-label"></label>
<input asp-for="EmpID" class="form-control" />
<span asp-validation-for="EmpID" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="RoleID" class="control-label"></label>
<input asp-for="RoleID" class="form-control" />
<span asp-validation-for="RoleID" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="DesignID" class="control-label"></label>
<input asp-for="DesignID" class="form-control" />
<span asp-validation-for="DesignID" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="SUBMIT" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
PLEASE HELP ME
I tried this.
public string InsertUserDatail(UserDetail usr)
{
string msg = string.Empty;
try
{
usr.CreatedBy = usr.Name;
usr.LastLoggedDate = NULL;
_context.UserDetails.Add(usr);
_context.SaveChanges();
msg = "success";
}
catch (Exception ex)
{
msg = "failed";
}
return msg;
}
and the form refuse to submit.