0

Problem

when remove any course from table list html his position in center or first it make problem

in index in database

Details

IF I have list of courses as following :

Delphi

Flash

Photoshop

IF I remove flash by JQUERY remove button then click save button

it delete flash and Photoshop

because there are wrong in delete courses by using remove function in jquery

if i remove php it is working without any problem because it is last item

suggestion

using map function when remove but how to do that

if there are any solution for that without using map no problem

i use model as following

public class Cusomemp2
    {
        public List<EmployeeCourse> empcourses { get; set; }

    }
}

i use for edit httppost

var result = db.Employees
             .Where(p => p.Id == custom.Id)
             .Include(c => c.EmployeeCourses)
             .FirstOrDefault();
        if (custom.empcourses.Any())
        {
            foreach (var ec in result.EmployeeCourses.ToList())//to remove existing EmployeeCourses
            {
                db.EmployeeCourses.Remove(ec);
                db.SaveChanges();

            }
        }

        result.EmployeeCourses = custom.empcourses;
     db.SaveChanges();

in model view my code as following by jquery

actually i need to modify code below using map function if there are any function do that never mind

     $(function () {
           //add courses using drop down
            var index = 0;
            $("#CourseId").change(function () {

                var id = $(this).val();
                var txt = $("#CourseId option:selected").text();

               $("#tb").append("<tr><td><input type = 'hidden' name='empcourses[" + index + "].CourseId' value='" + id + "'/></td><td>" + txt + "</td><td><input type='button' value='remove' class='r'</td></tr>")

                index++;

            });
            $("#tb").on("click", ".r", function () {
              // remove function
                $(this).parent().parent().remove();

                $(this).parent().prev().prev().remove();
             });

// retrieve data in edit view using ajax
            $.ajax({
                url: "/Employee/getcoursesbyempid",
                data:{x:$("#hid").val()},
                success: function (res) {
                    $.each(res, function (i, e) {

                          $("#tb").append("<tr><td><input type = 'hidden' name='empcourses[" + index + "].CourseId' value='" + e.Id + "'/></td><td>" + e.CourseName + "</td><td><input type='button' value='remove' class='r'</td></tr>")

                        index++;
                    });
                }

            })
        });

see image below to understand what i need enter image description here

  • Rather that trying to rename indexers so they are zero-based and consecutive, just include a hidden input for the indexer which allows you to post back non-consecutive indexers (refer [this answer](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796) for an explanation (in you case it will be ` –  Sep 17 '16 at 23:26
  • But your UI does not really make sense. Why not render a checklist box listing all courses and bind the checkbox to a (say) `bool IsSelected` property so you can just select/unselect courses in the view (no javascript required and you get strong typed binding). Refer [this answer](http://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416) for an example –  Sep 17 '16 at 23:29
  • how to make hidden can you update my post if possible – ahmed salah Sep 17 '16 at 23:35
  • You need to study the links I gave you, particularly the 2nd one –  Sep 17 '16 at 23:50
  • what you mean 2nd one – ahmed salah Sep 17 '16 at 23:55
  • The link in my second comment –  Sep 17 '16 at 23:58
  • this is different case about my case – ahmed salah Sep 18 '16 at 00:19
  • can you help me in that if possible – ahmed salah Sep 18 '16 at 00:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/123615/discussion-between-stephen-muecke-and-ahmed-salah). –  Sep 18 '16 at 00:21

0 Answers0