Visual Studio 2013,ASP.NEt MVC 5 Identity 请有人帮助我如何通过控制器保存数据库中的信息。让我解释一下......我想要发生以下事情: 记录的用户可以更新他的教育信息。在字段中进行更改后,他将按下保存按钮,信息将保存在数据库中。当我使用默认的aspnet用户类这样做时,这工作正常,我采取用户管理器。更新方法和信息已保存,但我无法使用任何其他表。如果有人帮助我,我会非常感激。 这是控制器类编辑方法
public async Task<ActionResult> Edit(string id, Education education)
{
if (!User.Identity.IsAuthenticated)
{
Response.Redirect("~/Account/Login");
}
var db = new ApplicationDbContext();
var educationdb = db.Edu.First(u => u.EducationID == education.EducationID);
educationdb.Qualification = education.Qualification;
educationdb.School = education.School;
educationdb.SchFrom = education.SchFrom;
educationdb.SchTo = education.SchTo;
educationdb.College = education.College;
educationdb.ClgFrom = education.ClgFrom;
educationdb.ClgTo = education.ClgTo;
educationdb.University = education.University;
educationdb.UniFrom = education.UniTo;
educationdb.Description = education.Description;
db.Entry(educationdb).State = System.Data.Entity.EntityState.Modified;
await db.SaveChangesAsync();
//return RedirectToAction("Index");
return View();
}
这是模型类:
namespace theme1.Models
{
public class Education
{
public string EducationID { get; set; }
public string UserID { get; set; }
public ApplicationUser User { get; set; }
public string Qualification { get; set; }
public string School { get; set; }
[DataType(DataType.Date)]
public DateTime SchFrom { get; set; }
[DataType(DataType.Date)]
public DateTime SchTo { get; set; }
public string College { get; set; }
[DataType(DataType.Date)]
public DateTime ClgFrom { get; set; }
[DataType(DataType.Date)]
public DateTime ClgTo { get; set; }
public string University { get; set; }
[DataType(DataType.Date)]
public DateTime UniFrom { get; set; }
[DataType(DataType.Date)]
public DateTime UniTo { get; set; }
public string Description { get; set; }
}
}
var educationdb = db.Edu.First(u =&gt; u.EducationID == education.EducationID);此行给出了一个错误异常详细信息:System.InvalidOperationException:Sequence不包含任何元素
来源错误: 第109行:var educationdb = db.Edu.First(u =&gt; u.EducationID == education.EducationID);
答案 0 :(得分:0)
而不是:
var educationdb = db.Edu.First(u => u.EducationID == education.EducationID);
试试这个:
var educationdb = db.Edu.Where(u => u.EducationID == education.EducationID).FirstOrDefault();
OR
而不是:
var educationdb = db.Edu.First(u => u.EducationID == education.EducationID);
试试这个:
var educationdb = db.Edu.FirstOrDefault(u => u.EducationID == education.EducationID);
答案 1 :(得分:0)
我怀疑你在ApplicationContext中错过了DBSet for Education模型。
如果这不能解决问题,请提供ApplicationContext类的代码吗?
答案 2 :(得分:0)
我不能评论你与Kartikeya的讨论,因为我不熟悉stackoverflow并缺乏声誉。
但是在你改变了你的lambda之后:
var educationdb = db.Edu.Where(u => u.EducationID ==
education.EducationID).FirstOrDefault();
从您与Kartikeya的讨论听起来您正在考虑创建而不是编辑。你不能编辑不存在的东西。
// GET: Model
public ActionResult Create()
{
return View();
}
在MVC 5中,通过右键单击Action并选择“添加视图”并插入所需信息来构建视图非常容易,当您创建视图时,为Create创建post方法: p>
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include ="EducationID, UserId....")] Education education)
{
if (ModelState.IsValid)
{
//any extra logic you might want
db.Edu.Add(education);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(education);
}