asp.net-mvc – 无法更改关系,因为一个或多个外键属性在MVC 4中不可为空
|
点击保存(更新)我的表单后,我收到此错误:
这是我的控制器(案例“保存”在swich couse问题): [HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(UserModel userModel,string act = null,int idx = 0)
{
using (var dbContext = new userDbEntities())
{
if (userModel.User == null)
{
userModel.User = new UsersTable();
}
var newUser = userModel.User.userID == 0;
userModel.CustomTypes = dbContext.CustomTypes.ToList();
switch (act)
{
case "addcustom":
userModel.User.CustomerTables.Add(new CustomerTable
{
CustomType = new CustomType(),UsersTable = userModel.User
});
break;
case "deletecustom":
userModel.User.CustomerTables.RemoveAt(idx);
break;
case "save":
foreach (var customer in userModel.User.CustomerTables)
{
customer.CustomType = dbContext.CustomTypes.Find(customer.CustomType.Id_NewCustomerType);
}
var dbUser = dbContext.UsersTables.Find(userModel.User.userID);
dbUser.TimeZoneId = userModel.User.TimeZoneId;
foreach (var custom in userModel.User.CustomerTables)
{
if (custom.CustomerID == 0)
dbUser.CustomerTables.Add(custom);
}
foreach (var custom in dbUser.CustomerTables.ToList())
{
var modelCustom =
userModel.User.CustomerTables.FirstOrDefault(o => o.CustomerID == custom.CustomerID);
if (modelCustom != null) //update it
{
custom.CustomType =
dbContext.CustomTypes.Find(modelCustom.CustomType.Id_NewCustomerType);
}
if (userModel.User.CustomerTables.All(o => o.CustomerID != custom.CustomerID))
dbUser.CustomerTables.Remove(custom);
}
dbContext.SaveChanges();
break;
} // end switch statements
return View("Edit",userModel);
}
}
任何想法是什么错误… 解决方法尝试如下所示.foreach (var child in modifiedParent.ChildItems)
{
context.Childs.Attach(child);
context.Entry(child).State = EntityState.Modified;
}
context.SaveChanges();
请参阅以下链接. http://social.msdn.microsoft.com/Forums/en-US/1833117c-7a93-4b69-a133-b7fd764db810/the-operation-failed-the-relationship-could-not-be-changed-because-one-or-more-of-the-foreignkey?forum=adodotnetentityframework (编辑:十堰站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 我应该在ASP.NET MVC中构建我的下一个Web应用程序吗?
- asp.net-mvc – 使用ASP.NET MVC进行项目组织的最佳实践
- asp.net core标签助手的高级用法TagHelper+Form
- ASP.NET MVC下Ajax.BeginForm方式无刷新提交表单实例
- asp.net-mvc – 从Api控制器内生成绝对的url to action
- asp.net – 无法加载文件或程序集System.Web.Http,版本= 5.
- asp.net – 在网站上放置广告的最佳做法?
- 从ASP.Net中的sessionID获取会话对象
- asp.net-mvc – Azure git部署 – 第二个程序集中缺少引用
- ASP.NET MVC AJAX文档在哪里?
- 这是一个bug?浮动操作被视为整数
- asp.net-mvc-3 – 如何避免使用MVC3 FileContent
- asp.net-mvc – 不要在ASP .NET MVC 4 BundleCon
- asp.net类序列化生成xml文件实例详解
- asp.net-mvc – 可以浏览DataAnnotations的自定义
- asp.net-mvc – 如何使用Visual Studio 2013和En
- asp.net – 我如何使用AJAX来确定用户的会话是否
- asp.net – 如何在页面加载时以“添加新”模式进
- asp.net – 干净的方式来防止输入按钮提交表单
- asp.net – ASP.NET中的“关键字不支持:”错误
