TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
ahmed salah
NA
530
148.7k
how to loop through hidden courses and remove from database
Sep 27 2016 12:37 PM
In my code i need to loop through hidden courses in jquery then remove it from employee course table in database sql in edit httppost under save button
see green font in place of my code i need
[HttpPost]
public
ActionResult Edit(Cusomemp2 custom)
{
var
result = db.Employees
.Where(p => p.Id == custom.Id)
.Include(c => c.EmployeeCourses)
.FirstOrDefault();
if
(custom.empcourses.Any())
{
//here i need to loop through data and remove hidden courses
}
result.Name = custom.Name;
result.EmployeeCourses = custom.empcourses;
db.SaveChanges();
return
View();
}
i use following model Cusomemp2 to represent data
public
class
Cusomemp2
{
public
int
Id { get; set; }
public
List<EmployeeCourse> empcourses { get; set; }
}
in edit view model my code as following
@model StudentCourses.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"
).val(
"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++;
});
}
})
});
</script>
</head>
<body>
<div>
@using (Html.BeginForm())
{
<div>
<input type=
"hidden"
value=
"@ViewBag.hid"
id=
"hid"
/>
Name: @Html.TextBoxFor(a => a.Name)
<br />
Courses:@Html.DropDownList(
"CourseId"
)
<br />
<table id=
"tb"
></table>
<input type=
"submit"
value=
"save"
/>
</div>
}
</div>
</body>
</html>
see image below it show what i need
Reply
Answers (
2
)
cofusing me the question
Fire textchanged event in javascript asp.net c#