如果在编辑httppost中删除,则无法保存课程的更改 [英] Cannot save changes of courses if remove in edit httppost
问题描述
问题
当删除课程然后点击提交不保存更改虽然
它通过jquery从客户端移除
详细信息
在每个员工的编辑视图中我需要通过删除或添加新课程进行更改
员工
如果我添加新课程然后点击提交按钮它保存我做什么
但如果我从课程中删除课程然后点击提交它将不会保存课程我>
删除
所以我需要检查我的代码中有什么问题
我的代码工作没有任何问题但只有问题
i无法保存在employeecourse表中的数据库中删除的课程点击提交
employeecourse表有Id,EmployeeId,CourseId
jquery删除客户端附带我的问题
代码
[HttpPost]
public ActionResult Edit(EditEmployeeVm model)
{
var \\ temp = db.Employees.FirstOrDefault(f = > f.Id == model.Id);
foreach ( var couseid in model.CourseIds)
{
db.EmployeeCourses.Add( new EmployeeCourse {CourseId = couseid,EmployeeId = emp.Id});
db.SaveChanges();
}
return View();
我的(自定义模型)视图模型使用
public class EditEmployeeVm
{
public int Id { set ; get ; }
public List< SelectListItem>课程{获取; set ; }
public int [] CourseIds { set 跨度>; get ; }
public 列表< coursevm> ExistingCourses { set ; get ; }
}
public class CourseVm
{
public int ID { set ; get ; }
public string 名称{ set 跨度>; get ; }
}
}
看下面我需要的图片
简单的文件共享和存储。 [ ^ ]
我尝试过:
无法保存课程更改如果删除编辑httppost
我希望您忘记发布删除控制器中的动作。但是你可能没有删除你的项目是因为你没有删除代码。
因为我对你的HTML一无所知/ javascript我将假设您知道如何进行ajax调用。在编辑操作中抛出此代码是一个坏主意并且会让人感到困惑,除非您在按下提交按钮之前尝试暂存删除。但同样,我不知道你要做什么,所以我会给出两个选择。
1)向你的控制器添加另一个动作以进行删除。 />
[HttpPost]
public JsonResult删除( int courseId)
{
var course = context.Courses.FirstOrDefault(m => m.Id == courseId);
context.Courses.Remove(course); // 这可能是错误的,它可能是context.Remove(course)...我使用的是存储库类所以我可以对我的类进行单元测试,这样我就可以直接编写这样的直接EF数据库访问代码了。
context.SaveChanges();
return Json( new {},JsonRequestBehavior.AllowGet);
}
2)如果您尝试在提交完成后暂停删除,那么您可以做两件事。可以连续删除并重新添加所有内容。或者,您可以在HTML中保留一个隐藏区域,以保留您的课程ID的总计,以便删除/删除。这可以放入你的控制器的编辑动作中
所以你要做的第一件事就是这样,隐藏在你的html中
< div id = 要隐藏的东西 >
< ; / div >
然后点击删除,你会在隐藏的东西中添加一个隐藏的输入,以保持ID的运行记录。
类似
// < span class =code-comment>使用.on而不是.click,我很懒,不喜欢它,虽然对于是时候我输入了这个我可以做的.on
( 。remove)。click( function (){
// 这是假设您将id附加到删除链接作为另一个名为data-id的属性,EX:< a href =data-id =2> ;删除< / a>
var idToRemove =
( this )。data( id);
// 我直接输入codeproject,语法可能有点不对,但你得到了想法跨度>
Problem
when remove course then click submit not save changes although
it removed from client side by jquery
Details
in edit view for every employee i need to do changes by remove or add new courses for
employee
if i add new courses then click submit button it save what i do
but if i remove course from courses then click submit it will not save courses i
removed
so that i need to check what is wrong in my code
my code working without any problem but only have problem
i cannot save courses removed in database in employeecourse table when click submit
employeecourse table have Id,EmployeeId,CourseId
jquery remove client side attached with my question
code
[HttpPost]
public ActionResult Edit(EditEmployeeVm model)
{
var emp = db.Employees.FirstOrDefault(f => f.Id == model.Id);
foreach (var couseid in model.CourseIds)
{
db.EmployeeCourses.Add(new EmployeeCourse { CourseId = couseid, EmployeeId = emp.Id });
db.SaveChanges();
}
return View();
my(custom model) view model using for that
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; }
}
}
to see what i need in image below
Simple File Sharing and Storage.[^]
What I have tried:
cannot save changes of courses if remove in edit httppost
I'm hoping you just forgot to post your delete action in your controller. But in the likelihood that you didn't, the reason why your item isn't deleting is because you have no delete code.
Since i know nothing about your html/javascript I'm going to assume you know how to make ajax calls. To throw this code in your edit action is a bad idea and would be confusing, that is unless you are trying to "Stage" the delete prior to pressing your submit button. But again, I have no idea what you are trying to do so i'll give two options.
1) Add another action to your controller for deletes.
[HttpPost] public JsonResult Delete(int courseId) { var course = context.Courses.FirstOrDefault(m=>m.Id == courseId); context.Courses.Remove(course); // this may be wrong, it may be context.Remove(course)...i use a repository class so I can unit test my classes so its been a while for me to write straight EF db access code like this. context.SaveChanges(); return Json(new { }, JsonRequestBehavior.AllowGet); }
2) If you are trying to stage the delete once submit has happened then you can do 2 things. Can can continuously delete and re-add everything. Or you can keep a hidden area in your HTML that keeps a running total of your course id's to delete/remove. This can be placed into your Edit action of your controller
So the first thing you'd do is have something like this, hidden in your html
<div id="things-to-hide"> </div>
Then on click of the remove, you would add a hidden input to the things-to-hide div that keeps a running tally of ID's to remove.
Something like
// use .on instead of .click, i'm lazy and don't feel like it, although for the time it took me to type this i could have done .on
(".remove").click(function() { // this is assuming you are attaching the id to the remove link as another attribute called data-id, EX: <a href="" data-id="2">Remove</a> var idToRemove =
(this).data("id"); // i typed straight into codeproject, syntax may be a little off but you get the idea
这篇关于如果在编辑httppost中删除,则无法保存课程的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!