none
Entity Framework does not save changes to database (database first) with no error RRS feed

  • Question

  • Controller

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Trackers.Models;
    
    namespace Trackers.Controllers
    {
        public class RegistrationController : Controller
        {
            
            // GET: Registration
            public ActionResult Index()
            {
                TrackersDBContext db = new TrackersDBContext();
    
                List<UserGroup> list = db.UserGroups.ToList();
                ViewBag.UserGroupList = new SelectList(list, "UserGroupId", "UserGroupName");
    
                return View();
            }
    
            [HttpPost]
    
            public ActionResult SaveRecord(RegisterViewModel model)
            {
                try
                {
                    TrackersDBContext db = new TrackersDBContext();
    
                    List<UserGroup> list = db.UserGroups.ToList();
                    ViewBag.UserGroupList = new SelectList(list, "UserGroupId", "UserGroupName");
    
                    User USER = new User();
                    USER.UserFullName = model.UserFullName;
                    USER.UserLogin = model.UserLogin;
                    USER.UserIC = model.UserIC;
                    USER.UserEmail = model.UserEmail;
                    USER.UserContact = model.UserContact;
                    USER.UserFaxNo = model.UserFaxNo;
                    USER.UserPassword = model.UserPassword;
                    USER.UserDesignation = model.UserDesignation;
                    USER.UserMMCRegistrationNo = model.UserMMCRegistrationNo;
                    USER.UserGroupId = model.UserGroupId;
    
                    db.Users.Add(USER);
                    db.SaveChanges();
    
                    int latestUserId = USER.UserId;
    
                    return RedirectToAction("Index");
                }
    
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
    }

    View

    @model Trackers.Models.RegisterViewModel
    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_LayoutRegistration.cshtml";
    }
    
    <h2>Index</h2>
    
    <div class="container" style="width:40%;margin-top:2%">
    
        @using (Html.BeginForm("SaveRecord", "Registration", FormMethod.Post))
        {
            @Html.TextBoxFor(model => model.UserFullName, new { @class = "form-control", @placeholder = "Name" })
            @Html.TextBoxFor(model => model.UserLogin, new { @class = "form-control", @placeholder = "Login Username" })
            @Html.TextBoxFor(model => model.UserIC, new { @class = "form-control", @placeholder = "IC Number" })
            @Html.TextBoxFor(model => model.UserEmail, new { @class = "form-control", @placeholder = "Email Address" })
            @Html.TextBoxFor(model => model.UserContact, new { @class = "form-control", @placeholder = "Contact Number" })
            @Html.TextBoxFor(model => model.UserFaxNo, new { @class = "form-control", @placeholder = "Fax No." })
            @Html.TextBoxFor(model => model.UserPassword, new { @class = "form-control", @placeholder = "Password" })
            @Html.TextBoxFor(model => model.UserDesignation, new { @class = "form-control", @placeholder = "Designation No." })
            @Html.DropDownListFor(model => model.UserGroupId, ViewBag.UserGroupList as SelectList, "--select--", new { @class = "form-control" })
            @Html.TextBoxFor(model => model.UserMMCRegistrationNo, new { @class = "form-control", @placeholder = "MMC Registration No." })
    
        <input type="submit" value="Submit" class="btn btn-block btn-primary" />
        }
    
    
    </div>
    

    model

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace Trackers.Models
    {
        public class RegisterViewModel
        {
            
            public string UserFullName { get; set; }
            public string UserIC { get; set; }
            public string UserPassword { get; set; }
            public string UserEmail { get; set; }
            public Nullable<int> UserGroupId { get; set; }
            public string UserMMCRegistrationNo { get; set; }
            public string UserLogin { get; set; }
            public string UserFaxNo { get; set; }
            public string UserDesignation { get; set; }
            public string UserContact { get; set; }
    
            public virtual UserGroup UserGroup { get; set; }
        }
    }

    please help me...I cannot proceed with my project


    Monday, February 12, 2018 6:44 PM

All replies