how to remove selected course from table EmployeeCourse when click save button [Resolved]

Posted by Ahmedsa under ASP.NET MVC on 9/30/2016 | Points: 10 | Views : 1446 | Status : [Member] | Replies : 1
Needs

Remove courses from EmployeeCourse table and jquery view when click save button if i remove course

code

in view

$("#tb").on("click", ".r", function () {

what i write here to remove in jquery client side
});
[HttpPost]
public ActionResult Edit(Cusomemp2 custom)
{
what i write here to remove from EmployeeCourse table server side

return View();
}

Details

IF i have 3 courses for employee Michel(interface as following)

c# remove button

SQL remove button

asp.net remove button

save button

I need when click remove button for SQL course remove it by jquery from client side then when click save button remove it from
EmployeeCourse table(server side)


i use this model

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


}

and when i need to retrieve courses for employee from EmployeeCourse table in

edit view i use this code below

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++;
});
}

})

function retrieve data in controller

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);
}

so that i need after retrieve data in edit view

remove course by jquery if i remove SQL course

then remove the SQ course from database in table EmployeeCourse

how i do that by code jquery and c# controller

EmployeeCourse table have three fields ( Id,CourseId,EmployeeId )


Course Table have ( Id,CourseName )


Employee Table have ( Id,Name )





Responses

Posted by: Rajnilari2015 on: 10/11/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