Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Team

I have a button that allow user to create course name, but my functionality to store that value to the database column i have is not saving and need some help.

What I have tried:

// Model
// db-schema lists.
    public class eNtsaCourses
    {
        [Key]
        public Guid? Id { get; set; }
        public string Course { get; set; }
        public string Nickname { get; set; }
        public string Term { get; set; }
        public string EnrolledAs { get; set; }
        public bool Published { get; set; }

    }


[Route("Home/CoursesRegistration")]
     public ActionResult CoursesRegistration( eNtsaRegCourses collection)
       {
           if(ModelState.IsValid)
           {

               cb.SaveChanges();
               return RedirectToAction("Courses", "Home");
           }

           return View(collection);
       }


//GET://Courses/Courses-All.
       [Route("Home/Courses")]
     public ActionResult Courses(string g)// Once user creates course, it must be stored
       {
           return View(g);
       }


// View
<div class="modal-footer">
                                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                                            <a class="btn btn-large btn-success" id="fire" href="@Url.Action("CoursesRegistration", "Home")">Create Courses</a>
                                            <script type="text/javascript" src="~/Scripts/jquery-3.4.1.js"></script>
                                            <script type="text/javascript">
                                                $('#fire').on('click', function (e) {
                                                        var url = '@Url.Action("CoursesRegistration", "eNtsaCourses")';
                                                        $(jQuery.noConflict);
                                                        $('#ModalContent').load(url, function (html) {
                                                         var form = $("#Modal-eNtsaCourses form");
                                                         $.validator.unobtrusive.parse(form);
                                                         $("#Modal-eNtsaCourses").modal('show');
                                                        form.submit(function () {
                                                        $.ajax({
                                                        url: this.action,
                                                         type: this.method,
                                                        data: $(this).serialize(),
                                                        success: function (result) {
                                                        $('#Modal-eNtsaCourses').modal('hide');
                                                var content = '@Url.Action("Courses", "eNtsaCourses")';
                                                      $('#ViewCourses').load(content);
                                                        }
                                                        });
                                                    return false;
                                                });
                                                });
                                                });

                                   
                                            </script>

                                        </div>
Posted
Comments
[no name] 19-Aug-20 12:54pm    
A "nullable" primary key? And you have no code that actually does an "insert".
gcogco10 19-Aug-20 12:57pm    
must i create ActionResult Add(eNtsaCourse courses) {.... //logic like var data = db.eNtsaCourses.Add();?}
DerekT-P 21-Aug-20 2:52am    
How do you know it is "not saving"? What exception is raised? Maybe it's in a transaction that's not committed? Maybe you're looking in the wrong place for it? What is "cb" and where's the "SaveChanges" code? (As judging by its name, that's probably the bit we need to see).
I suspect if you think a bit about these questions you'll be able to debug it yourself. BTW in "what I have tried" we really expect to see the steps YOU have already taken to resolve the problem...)
gcogco10 25-Aug-20 5:41am    
Derek the issue is it does not hit Httpost to my controller as it goes within jquery side. What i want exactly when a user clicks it must fire and save those details to my table. currently it does not.
DerekT-P 25-Aug-20 13:49pm    
Then step through the Javascript to find out why not. You've not told us what #fire is, what the form is that you're trying to submit, or described what should actually happen. You'll need to do that - and more - or resolve it yourself.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900