I try to run my CRUD application for multiple model of Admin information on datatables using JavaScript + EF MVC but not luck to see result successfully.
I am surfing net , read many the forum and also follow step from youtuber but i m
hang for this two function "Add" and "Edit" on few weeks.
Please help me to check and solve which coding part i m wrong and also check jquery plugin too . Thank you for advanced.
What I have tried:
1) "Add New" function cannot back to same screen when pressed "Submit" button.
2) "Edit" function cannot display out information when pressed "Edit"as pencil icon button
Below is my Coding:
Model parts:
- public partial class ADMIN
- {
- public string ADMIN_ID { get; set; }
- public string ADMIN_NAME { get; set; }
- public string ADMIN_PASSWORD { get; set; }
- public string ADMIN_EMAIL { get; set; }
- public string ADMIN_STATUS { get; set; }
- public string ADMIN_COMMENT { get; set; }
- public virtual STATUS STATUS { get; set; }
- }
- public partial class STATUS
- {
- public string STATUS_ID { get; set; }
- public string STATUS_DESC { get; set; }
- }
- @model ABC.Models.ADMIN
- @{
- Layout = null;
- }
- @using (Html.BeginForm("StoreOrEdit", "Admin", FormMethod.Post, new { onsubmit = "return SubmitForm(this)" }))
- {
- @*@Html.HiddenFor(model => model.ADMIN_ID)*@
-
@Html.Label("ID", new { @class = "control-label" }) @Html.Label(":", new { @class = "control-label" }) @Html.EditorFor(model => model.ADMIN_ID, new { htmlAttributes = new { @class = "form-control", @placeholder = "ID" } }) - @Html.ValidationMessageFor(model => model.ADMIN_ID, "", new { @class = "text-danger" })
@Html.Label("Name", new { @class = "control-label" }) @Html.Label(":", new { @class = "control-label" }) @Html.EditorFor(model => model.ADMIN_NAME, new { htmlAttributes = new { @class = "form-control" } }) - @Html.ValidationMessageFor(model => model.ADMIN_NAME, "", new { @class = "text-danger" })
@Html.Label("Pasword", new { @class = "control-label" }) @Html.Label(":", new { @class = "control-label" }) @Html.EditorFor(model => model.ADMIN_PASSWORD, new { htmlAttributes = new { @class = "form-control" } }) - @Html.ValidationMessageFor(model => model.ADMIN_PASSWORD, "", new { @class = "text-danger" })
@Html.Label("Email", new { @class = "control-label" }) @Html.Label(":", new { @class = "control-label" }) @Html.EditorFor(model => model.ADMIN_EMAIL, new { htmlAttributes = new { @class = "form-control", @placeholder = "Email", @style = "width:580px;" } }) - @Html.ValidationMessageFor(model => model.ADMIN_EMAIL, "", new { @class = "text-danger" })
@Html.Label("Status", new { @class = "control-label" }) @Html.Label(":", new { @class = "control-label" }) @Html.EditorFor(model => model.ADMIN_STATUS, new { htmlAttributes = new { @class = "form-control" } }) - @Html.ValidationMessageFor(model => model.ADMIN_STATUS, "", new { @class = "text-danger" })
@Html.Label("Comment", new { @class = "control-label" }) @Html.Label(":", new { @class = "control-label" }) @Html.EditorFor(model => model.ADMIN_COMMENT, new { htmlAttributes = new { @class = "form-control" } }) - class="form-group">
- "submit" value="Submit" class="btn btn-success" />
- "reset" value="Reset" class="btn btn-warning" />
- }
- @model IEnumerable
- @{
- ViewBag.Title = "Admin Profile";
- Layout = "~/Views/Shared/_LayoutPage2.cshtml";
- }
- "~/Content/dataTables.bootstrap4.min.css" rel="stylesheet" />
- "~/css/font-awesome.min.css" rel="stylesheet" />
- class="btn btn-primary" style="margin-bottom:10px" onclick="PopupForm('@Url.Action("StoreOrEdit","Admin")')">class="fa fa-plus">Add New
"adminTable" class="table table-striped table-bordered" style="width:100%">
- @Html.DisplayName("ID")
- @Html.DisplayName("Name")
- @Html.DisplayName("Password")
- @Html.DisplayName("Email")
- @Html.DisplayName("Status")
- @Html.DisplayName("Comment")
- @section scripts{
- }
Controller part:
- //Create Method for Insert and Update
- [HttpGet]
- public ActionResult StoreOrEdit(string adminModel_Id = " ")
- {
- if (adminModel_Id == null)
- return View(new ADMIN());
- else
- {
- var query = (from objAdmin in SqlDbContext.ADMINs
- join objStatus in SqlDbContext.STATUS on objAdmin.ADMIN_STATUS equals objStatus.STATUS_ID
- select new
- {
- ADMIN_ID = objAdmin.ADMIN_ID,
- ADMIN_NAME = objAdmin.ADMIN_NAME,
- ADMIN_PASSWORD = objAdmin.ADMIN_PASSWORD,
- ADMIN_EMAIL = objAdmin.ADMIN_EMAIL,
- STATUS_DESC = objStatus.STATUS_DESC,
- ADMIN_COMMENT = objAdmin.ADMIN_COMMENT
- }).FirstOrDefault(x => x.ADMIN_ID == adminModel_Id);
- return View(query);
- }
- }
- [HttpPost]
- public ActionResult StoreOrEdit(ADMIN adminModel)
- {
- try
- {
- if (SqlDbContext.ADMINs.Where(x => x.ADMIN_ID.Equals(adminModel.ADMIN_ID)).Count() < 1)
- {
- ADMIN adminObj = new ADMIN();
- adminObj.ADMIN_ID = adminModel.ADMIN_ID;
- adminObj.ADMIN_NAME = adminModel.ADMIN_NAME;
- adminObj.ADMIN_EMAIL = adminModel.ADMIN_EMAIL;
- adminObj.ADMIN_STATUS = adminModel.ADMIN_STATUS;
- adminObj.ADMIN_COMMENT = adminModel.ADMIN_COMMENT;
- SqlDbContext.ADMINs.Add(adminModel);
- SqlDbContext.SaveChanges();
- return Json(new { success = true, message = "Saved Successfully", JsonRequestBehavior.AllowGet });
- }
- else
- {
- // db.Entry(adminOb).State = EntityState.Modified;
- ADMIN adminObj = SqlDbContext.ADMINs.SingleOrDefault(x => x.ADMIN_STATUS == "01" && x.ADMIN_ID == adminModel.ADMIN_ID);
- adminObj.ADMIN_NAME = adminModel.ADMIN_NAME;
- adminObj.ADMIN_EMAIL = adminModel.ADMIN_EMAIL;
- adminObj.ADMIN_STATUS = adminModel.ADMIN_STATUS;
- adminObj.ADMIN_COMMENT = adminModel.ADMIN_COMMENT;
- SqlDbContext.SaveChanges();
- return Json(new { success = true, message = "Updated Successfully", JsonRequestBehavior.AllowGet });
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.InnerException);
- throw;
- }
- }
Replies
Know the answer? Post it — somebody with the same question will find it here.