私は編集したいプロジェクトモデルを持っています。問題は、所有者への私の外部キーが編集後にnullに設定されていることです。MVC 6外部キーを持つモデルを編集する
また、ポストアクションintプロジェクトを作成するとき、私は所有者を手動で追加します。
私はそれを編集した後、私の外部キーがなぜリセットされたのかわかりません。私はそれを一人で残しておく方法を知らないのです。
これを行う正しい方法は何ですか?
public class Project
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string OwnerId { get; set; }
[ForeignKey("OwnerId")]
public virtual ApplicationUser Owner { get; set; }
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,Description,Name")] Project project)
{
if (id != project.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(project);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ProjectExists(project.Id))
return NotFound();
else
throw;
}
return RedirectToAction("Index");
}
return View(project);
}
Bind属性に 'OwnerId'を追加し、ビューの' 'を追加しました。 – korn3l
私はViewModelアプローチの使い方を知らなかった。あなたはそれの例を挙げることができますか? – korn3l
この質問をご覧ください:http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc –