how to remove courses store in ids in edit post controller [Resolved]

Posted by Ahmedsa under ASP.NET MVC on 10/8/2016 | Points: 10 | Views : 474 | Status : [Member] | Replies : 1
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 model 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();
}

http://www.mediafire.com/view/awe5t3xlh6ghbgx/remove_ids_courses.jpg
see image above for details




Responses

Posted by: Rajnilari2015 on: 10/9/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Try this,

using (var context = new CourseContext())
{
var course = context.Authors.Find(custom2.Id); //specify the CourseId
context.Courses.Remove(course); //removes the entity from the Courses Entities/Collections
context.SaveChanges();
}

We have already written an article on the same. Please find it here http://www.dotnetfunda.com/articles/show/3314/crud-using-code-first-approach-of-entity-framework-ef

Hope that helps


--
Thanks & Regards,
RNA Team

Ahmedsa, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response