Asp.net mvc 如何在EmployeeCourse表中保存课程更改
我需要什么 我需要保存EmployeeCourse表中课程的更改 通过从EmployeeCourse表中删除员工的所有课程 然后在更改后添加课程 问题 删除后在课程中添加更改时,save changes()函数中出现错误 {“更新条目时出错 INSERT语句与外键约束冲突 \“FK_EmployeeCourse_Course\”。数据库“mycourse\”中发生冲突 表“dbo.Course\”的列“Id”。\r\n语句已终止。“} 在提交按钮下编辑[httppost]Asp.net mvc 如何在EmployeeCourse表中保存课程更改,asp.net-mvc,linq-to-sql,linq-to-entities,entity-framework-5,Asp.net Mvc,Linq To Sql,Linq To Entities,Entity Framework 5,我需要什么 我需要保存EmployeeCourse表中课程的更改 通过从EmployeeCourse表中删除员工的所有课程 然后在更改后添加课程 问题 删除后在课程中添加更改时,save changes()函数中出现错误 {“更新条目时出错 INSERT语句与外键约束冲突 \“FK_EmployeeCourse_Course\”。数据库“mycourse\”中发生冲突 表“dbo.Course\”的列“Id”。\r\n语句已终止。“} 在提交按钮下编辑[httppost] public Acti
public ActionResult Edit(EditEmployeeVm model)
{
var emp = db.Employees.FirstOrDefault(f => f.Id == model.Id);
// remove all courses
foreach (EmployeeCourse eec in emp.EmployeeCourses.ToList())
{
var ec = db.EmployeeCourses.Find(eec.Id);
db.EmployeeCourses.Remove(ec);
}
// add courses again
foreach (var couseid in model.CourseIds)
{
db.EmployeeCourses.Add(new EmployeeCourse { CourseId = couseid, EmployeeId = emp.Id });
}
db.SaveChanges();
}
调试结果
调试添加如下图所示
详细信息
两个表之间的一对多关系
雇员课程
Id(主键)课程Id(外键)员工Id(外键)
数据库中的课程表
Id(pk)CourseName
I using following view model for edit to get CourseIds
@model StudentCourses.Models.EditEmployeeVm
$(function () {
$(document).on("click", ".remove", function (e) {
e.preventDefault();
$(this).closest(".course-item").remove();
});
$('#AvailableCourses').change(function () {
var val = $(this).val();
var text = $("#AvailableCourses option:selected").text();
var existingCourses = $("input[name='CourseIds']")
.map(function () { return this.value; }).get();
if (existingCourses.indexOf(val) === -1) {
var newItem = $("<div/>").addClass("course-item")
.append(text + ' <a href="#" class="remove" data-id="' + val + '">Remove </a>');
newItem.append('<input type="text" name="CourseIds" value="' + val + '" />');
$("#items").append(newItem);
}
});
});
</script>
在“编辑获取”操作中,您为CourseVm使用了错误的id。它应该是课程Id,但您正在设置
EmployeeCourse
记录/实体Id
这应该可以解决它
vm.ExistingCourses = db.EmployeeCourses.Where(g => g.EmployeeId == id)
.Select(f => new CourseVm
{
Id = f.Course.Id,
Name = f.Course.CourseName
}).ToList();
461在
课程
表格中是否为有效的courseId?抱歉,我混淆了461在课程表格中无效Course表格有两个值1 python 2 delphican any one help meSo您从表单中发送了错误的值。您可能需要修复获取值461的位置。请确保您是从课程表中获取Id。非常感谢您,它已解决,但如果可能,在删除所有课程时,仍有一点未解决,然后单击“提交它给我异常错误对象引用未设置为对象实例”。在这行foreach中(model.CourseIds中的var couseid)
public class EditEmployeeVm
{
public int Id { set; get; }
public List<SelectListItem> Courses { get; set; }
public int[] CourseIds { set; get; }
public List<CourseVm> ExistingCourses { set; get; }
}
public class CourseVm
{
public int Id { set; get; }
public string Name { set; get; }
}
}
public ActionResult Edit(int id)
{
var vm = new EditEmployeeVm { Id = id };
var emp = db.Employees.FirstOrDefault(f => f.Id == id);
vm.Name = emp.Name;
vm.ExistingCourses = db.EmployeeCourses
.Where(g => g.EmployeeId == id)
.Select(f => new CourseVm
{
Id = f.Id,
Name = f.Course.CourseName
}).ToList();
vm.CourseIds = vm.ExistingCourses.Select(g => g.Id).ToArray();
vm.Courses = db.Courses.Select(f => new SelectListItem
{
Value = f.Id.ToString(),
Text = f.CourseName
}).ToList();
return View(vm);
}
vm.ExistingCourses = db.EmployeeCourses.Where(g => g.EmployeeId == id)
.Select(f => new CourseVm
{
Id = f.Course.Id,
Name = f.Course.CourseName
}).ToList();