none
how to remove courses store in ids in edit post controller RRS feed

  • Question

  • In edit http post i need to remove courses stored in ids variable

    note i need remove courses selected not edit
    in jquery i store values of removed courses in ids variable
    suppose i removed photoshop and flash it will store value of 3,4 in ids variable
    below code when click remove it save values of courses removed in variable ids as array
     
    $("#tb").on("click", ".r", function () {  
    $(this).parent().parent().hide();  
    $(this).parent().prev().prev().find("input").addClass("remove");  
    var ids = [];  
    var i = 0;  
    $(".remove").each(function () {  
    ids[i] = $(this).val();  
    i++;  
    });  
    for (var k = 0; k < ids.length; k++) {  
    alert(ids[k]);  
    }  
    });  

    i show courses per employee from table EmployeeCourse in edit get as following

    var index = 0;  
    $.ajax({  
    url: "/Employeedata/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++;  
    });  
    }  
    })  
    public JsonResult getcoursesbyempid(int x)  
    {  
    db.Configuration.ProxyCreationEnabled = false;  
    var data = db.EmployeeCourses.Where(a => a.EmployeeId == x).Join(db.Courses, a => a.CourseId, b => b.Id, (a, b) => new { Id = a.CourseId, CourseName = b.CourseName });  
    return Json(data, JsonRequestBehavior.AllowGet);  
    }  

     my mode using as following

    public class Cusomemp2  
    {  
        public int Id { get; set; }  
        public string Name { get; set; }  
        public List<EmployeeCourse> empcourses { get; set; }  
    }  

     

    when remove courses in edit HTTPPOST what i write here

    [HttpPost]  
    public ActionResult Edit(Cusomemp2 custom)  
    {  
    //what i write to remove courses saved in ids from table EmployeeCourse  
    return View();  
    }  


    see image above for details
    Friday, October 7, 2016 3:44 PM

All replies

  • can you help me in that

    my problem in server side

    [HttpPost]  
    public ActionResult Edit(Cusomemp2 custom)  
    {  
    //what i write to remove courses saved in ids from table EmployeeCourse  
    return View();  
    }  

     how to represent values found in ids variable in jquery in controller

    Saturday, October 8, 2016 7:59 PM
  • Hi eng-ahmed-salah,

    Could you please provide complete view, controller, models, if possible, could you please provide a simple demo about this issue via OneDrive. we'll try to reproduce your issue on our side and try to find a solution to resolve it.

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, October 10, 2016 2:35 AM
    Moderator
  • in this link below you can see all files for my project

    http://www.mediafire.com/file/htc0d7elap9huof/full_project_code.rar

    Wednesday, October 12, 2016 10:10 PM
  • i attached my file of project in link above

    can you help me in that if possible

    Friday, October 14, 2016 1:48 PM
  • Hi eng-ahmed-salah,

    I have download your code and reproduce your issue on my side, it seems that we need to add a hidden control to restore the ids variable. like this:

    @model MVCDemo.Models.Cusomemp2
    @{
        Layout = null;
    }
    <!DOCTYPE html>
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Edit</title>
        <script src="~/scripts/jquery-1.10.2.js"></script>
        <script>
            $(function () {
    
                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 () {
                      $(this).parent().parent().hide();
                      $(this).parent().prev().prev().find("input").addClass("remove");
                      var ids = [];
                      var i = 0;
                      $(".remove").each(function () {
                          ids[i] = $(this).val();
                          i++;
                      });
                      $("#ids").val(ids);
                      //for (var k = 0; k < ids.length; k++) {
                      //    alert(ids[k]);
                      //}
                 });
    
    
                $.ajax({
                    url: "/Employeedata/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++;
                        });
                    }
    
                })
            });
        </script>
    </head>
    <body>
        <div>
            @using (Html.BeginForm())
            {
                <div>
                    <input type="hidden" value="@ViewBag.hid" id="hid" />
                    <input type="hidden" name="ids" id="ids" />
                    Name: @Html.TextBoxFor(a => a.Name)
                    <br />
                    Courses:@Html.DropDownList("CourseId")
                    <br />
                    <table id="tb"></table>
                    <input type="submit" value="save" />
                </div>
                <table id="tb"></table>
            }
        </div>
    </body>
    </html>
    
    

    Then we could retrieve ids like this:

    [HttpPost]
            public ActionResult Edit(FormCollection form)
            {
                string ids = form["ids"];
    
    
                //what i write here to remove courses selected
                return View();
            }

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 18, 2016 3:18 AM
    Moderator
  • Thank you for reply

    i put break point in this line

    string ids = form["ids"];

    then remove two course by jquery it displayed in text box i added

    after this click on save button

    it give me null value in string ids variable

    how to solve that problem


    Tuesday, October 25, 2016 10:03 PM
  • can any one help me in answering question above
    Friday, October 28, 2016 1:43 AM
  • Hi eng-ahmed-salah,

    >>can any one help me in answering question above

    Do your remove the item? Based on your description, I do a operation, it seems ok, I could get the ids value.

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 31, 2016 8:23 AM
    Moderator