Can't insert data into database in MVC
My Code
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DLFeedback360.Model
{
[Table("UserDetails")]
public class UserDetail
{
[Key]
public string RowID { get; set; }
public string UserID { get; set; }
public string Password { get; set; }
public string Name { get; set; }
public int 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; }
}
}
//////////////////////////////////////////////////
using DLFeedback360.Abstract;
using DLFeedback360.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DLFeedback360.Repository
{
public class UserDetailDataManagement : IUserDetailManagement
{
UserDetailDbContext _context = new UserDetailDbContext();
public List<UserDetail> GetAllDetails()
{
List<UserDetail> lst = new List<UserDetail>();
try
{
lst = _context.UserDetails.ToList();
}
catch (Exception ex)
{
}
return lst;
}
public string InsertUserDatail(UserDetail usr)
{
string msg = string.Empty;
try
{
_context.UserDetails.Add(usr);
_context.SaveChanges();
msg = "success";
}
catch (Exception ex)
{
msg = "failed";
}
return msg;
}
}
}
/////////////////////////////////////////
using DLFeedback360.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DLFeedback360.Abstract
{
public interface IUserDetailManagement
{
string InsertUserDatail(UserDetail usr);
List<UserDetail> GetAllDetails();
}
};
/////////////////////////////////////////////
using DLFeedback360.Model;
using DLFeedback360.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BLFeedback360
{
public class UserDetailDataManagement
{
UserDetailDataManagement objBL = new UserDetailDataManagement();
public List<UserDetail> GetAllDetails()
{
List<UserDetail> lst = new List<UserDetail>();
try
{
lst = objBL.GetAllDetails();
}
catch (Exception ex)
{
string rr = ex.Message;
}
return lst;
}
public string InsertUserDatail(UserDetail usr)
{
string msg = string.Empty;
try
{
// In case before passing the prod object to DL in line 40, you want to do any data manipulation
objBL.InsertUserDatail(usr);
msg = "success";
}
catch (Exception ex)
{
msg = "failed";
}
return msg;
}
}
}
////////////////////////////////////////////////////
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DLFeedback360.Model
{
public class UserDetailDbContext : DbContext
{
public DbSet<UserDetail> UserDetails { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Server=DESKTOP-1M2N4KR\\HW;Database=Rocky;Integrated Security=true;Trusted_Connection=True;TrustServerCertificate=True;");
}
base.OnConfiguring(optionsBuilder);
}
}
}
//////////////////////////////////////////////////////
using DLFeedback360.Model;
using DLFeedback360.Repository;
using Microsoft.AspNetCore.Mvc;
namespace Feedback360.Controllers
{
public class UserDetailController : Controller
{
//UserDetailDataManagement objBL = new UserDetailDataManagement();
UserDetailDataManagement objBL = new UserDetailDataManagement();
public IActionResult Index()
{
List<UserDetail> lstProd = objBL.GetAllDetails();
return View(lstProd);
}
[HttpGet]
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(UserDetail usr)
{
string msg = objBL.InsertUserDatail(usr);
if (msg == "success")
{
return RedirectToAction("Index");
}
ViewBag.ErrorInfo = "Data not saved, try again latter";
return View();
}
}
}
@model IEnumerable<DLFeedback360.Model.UserDetail>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.UserID)
</th>
<th>
@Html.DisplayNameFor(model => model.Password)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.MobNumber)
</th>
<th>
@Html.DisplayNameFor(model => model.EmailID)
</th>
<th>
@Html.DisplayNameFor(model => model.EmpID)
</th>
<th>
@Html.DisplayNameFor(model => model.RoleID)
</th>
<th>
@Html.DisplayNameFor(model => model.DesignID)
</th>
<th>
@Html.DisplayNameFor(model => model.LastLoggedDate)
</th>
<th>
@Html.DisplayNameFor(model => model.CreatedBy)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Password)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.MobNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmailID)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmpID)
</td>
<td>
@Html.DisplayFor(modelItem => item.RoleID)
</td>
<td>
@Html.DisplayFor(modelItem => item.DesignID)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastLoggedDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.CreatedBy)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</tbody>
</table>
</body>
</html>
//////////////////////////////////////////////
@model DLFeedback360.Model.UserDetail
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Create</title>
</head>
<body>
@ViewBag.ErrorInfo
<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">
<label asp-for="LastLoggedDate" class="control-label"></label>
<input asp-for="LastLoggedDate" class="form-control" />
<span asp-validation-for="LastLoggedDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="CreatedBy" class="control-label"></label>
<input asp-for="CreatedBy" class="form-control" />
<span asp-validation-for="CreatedBy" 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>
</body>
</html>