私はちょうど質問をしたい、私はASP.Net MVCの初心者です。ActionResult 2つのテーブル(1対1)を持つEntity Frameworkを使用してMVCで編集しますか?
これは私の場合です:
私は2つのテーブルを持っている...
学生表:ID(主キー)、名前、およびクラス バッグ表:ID(主キー)、ブランド
ID(学生)ID(袋)との関係 つの関係の一つには、..
これは私のコントローラのコードである
// POST: Students/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Nama,Kelas,Brand")] Student Student, Bag Bag)
{
if (ModelState.IsValid)
{
db.Students.Add(Student);
Bag.Id = Student.Id;
db.Bag.Add(Bag);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.Id = new SelectList(db.Bag, "Id", "Brand", Student.Id);
return View(Student);
}
// POST: Students/Edit/5
// To protect from overposting attacks, please enable the specific you to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,Nama,Kelas,Brand")] Student Student, Bag Bag)
{
if (ModelState.IsValid)
{
db.Entry(Student).State = EntityState.Modified;
//db.Entry(Bag).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.Id = new SelectList(db.Bag, "Id", "Brand", Student.Bag.Id);
ViewBag.Id = new SelectList(db.Students, "Id", "Nama", Student.Id);
return View(Student);
}
with ActionResult ID、名前、ブランドを追加できますが、フィールドの編集/更新方法が混乱します。ブラウザで編集をクリックするとID、名前、クラスは編集できますが、ブランドdidn
2つのテーブルで編集する方法は何ですか? 誰でも自分のコードを修正できますか?
ご清聴ありがとうございました:)
まず ...あなたの優れた応答をありがとう、私は急いで、問題を投稿して、と私はそのためのmodel..sorryについてのスクリーンショットはなかった。.. あなたは「 私に言ったことに基づいて、変更したいエンティティのプロパティのみを含むビューモデルを使用する方が良いです.. " 私はそれについての例やリファレンスを教えていただけますか?私はまだMVCを学んでいるので、googleで検索し、alliteboo *からmvcに関する電子ブックを読んでいますが、1対1の関係の例は見つけられませんでした。私はCRUD関数を作成する方法について、 最後に、私はあなたに感謝します。 – redburn