locked
submit button click not save courses data when i remove course by jquery RRS feed

  • Question

  • User1601964810 posted

    Problem

    when i use remove button to remove course.

    it will remove from interface(in client side by jquery) but not save course

    removed in database(sql server server side) if i click submit button .

    when click submit button to save courses in edit view[HTTP POST] it save

    success if i adding new courses

    **Meaning i can save new course but if i remove course i cannot save**

    image show

    http://www.mediafire.com/view/0trg2l78b24bjr1/wrong2.jpg


    code

    in edit view get it show code for remove course by jquery

    @model WebCourse.Models.EditEmployeeVm
    
    
    
    <script>
    $(function () {
    $(document).on("click", ".remove", function (e) {
    e.preventDefault();
    $(this).closest(".course-item").remove();
    });
    $('#AvailableCourses').change(function () {
    var val = $(this).val();
    var text = $("#AvailableCourses option:selected").text();
    var existingCourses = $("input[name='CourseIds']")
    .map(function () { return this.value; }).get();
    
    if (existingCourses.indexOf(val) === -1) {
    // Not exist. Add new
    var newItem = $("<div/>").addClass("course-item")
    .append(text + ' <a href="#" class="remove" data-id="' + val + '">Remove </a>');
    newItem.append('<input type="text" name="CourseIds" value="' + val + '" />');
    
    $("#items").append(newItem);
    }
    });
    });
    </script>
    </head>
    <body>
    <div>
    @using (Html.BeginForm())
    {
    
    @Html.HiddenFor(g => g.Id)
    @Html.DropDownList("AvailableCourses", Model.Courses, "Select")
    <div id="items"></div>
    foreach (var c in Model.ExistingCourses)
    {
    <div class="course-item">
    @c.Name <a href="#" class="remove" data-id="@c.Id">Remove </a>
    <input type="text" name="CourseIds" value="@c.Id" />
    </div>
    }
    
    }
    </div>
    </body>
    </html>
    in edit view httppost that update data in employeecourse table
    [HttpPost]
    public ActionResult Edit(EditEmployeeVm model)
    
    {
    var emp = db.Employees.FirstOrDefault(f => f.Id == model.Id);
    
    foreach (var couseid in model.CourseIds)
    {
    db.EmployeeCourses.Add(new EmployeeCourse { CourseId = couseid, EmployeeId = emp.Id });
    db.SaveChanges();
    
    }
    
    return View();
    }
    
    
    



    Monday, September 5, 2016 12:43 PM

All replies

  • User1601964810 posted

    can you help me in answering this question

    Monday, September 5, 2016 11:52 PM
  • User-1142886626 posted

    Hi ahmed.barbary,

    submit button click not save courses data when i remove course by jquery

    As we all know, the jQuery remove () method removes the selected elements, including all text and child nodes. This method also removes data and events of the selected elements.

    If you want remove the elements without removing data and events, use the detach () method instead.

    If you want remove only the content from the selected elements, use the empty () method.

    You could refer to the following link.

    https://api.jquery.com/remove/

    https://api.jquery.com/detach/

    Hope this can help you. If you have any question and confusion about the problem. Please don't hesitate to let me know.

    Best Regards,

    Ailleen

    Tuesday, September 6, 2016 8:30 AM
  • User1601964810 posted

    i use dettach in place of remove nothing changed problem before still exist and not solved until now

    Tuesday, September 6, 2016 10:40 AM
  • User-1142886626 posted

    Hi ahmed.barbary,

    You could take a look with Glimpse. It provides detailed performance, debugging and diagnostic information for ASP.NET apps. You need debug your code to find whether edit is performed when you add and remove.

    Following link is about how to use it.

    http://www.asp.net/mvc/overview/performance/profile-and-debug-your-aspnet-mvc-app-with-glimpse

    Best Regards,

    Ailleen

    Wednesday, September 7, 2016 11:38 AM