これは私を狂ってしまう。私は一歩一歩進みます。MVC - EFバインディングがアップデートに反映されない
は、これは私のモデルである:
public class Debate : BaseEntity
{
public string Title { get; set; }
public string Description { get; set; }
public virtual Team Team1 { get; set; }
public virtual Team Team2 { get; set; }
//Problem here.
public virtual Cathegory Cathegory { get; set; }
}
私の編集(ポスト)では
public class DebateEditVm : BaseVm
{
[Required]
public string Title { get; set; }
[Required]
public string Description { get; set; }
public TeamVm Team1 { get; set; }
public TeamVm Team2 { get; set; }
//The Value of the selected one.
public Guid Cathegory { get; set; }
//List of different cathegories. I show this in the view with a @Html.DropDownListFor
public List<CathegoryVm> Cathegories { get; set; }
public DebateAltaVm()
{
Team1 = new TeamVm();
Team2 = new TeamVm();
}
}
私は次のことを持っている(編集時)これは私のViewModelにあります。
public ActionResult Edit([Bind(Include = Id,Title,Description,Team1,Team2,Cathegory")] DebateEditVm debate)
{
//Automapper from VM to MODEL
var debateEntity = Mapper.Map<Debate>(debate);
//Either with this commented or not wont Update the Cathegory of the debate!!!!!!!!
//debateEntity.Cathegory = Db.Categorias.Find(debateEntity.Cathegory.Id);
Db.Entry(debateEntity).State = EntityState.Modified;
Db.Entry(debateEntity.Team1).State = EntityState.Modified;
Db.Entry(debateEntity.Team2).State = EntityState.Modified;
Db.SaveChanges();
return RedirectToAction("Index");
}
return View(debate);
}
Automapperだから新しい値がVMへバインドさと私はディベートを更新しようとすると、すべての変更がCathegory(私はコメント行でも試してみました)
を除いて良いですされ Mapper.CreateMap<Debate, DebateEditVm>()
.ForMember(m => m.Cathegory, opt=>opt.MapFrom(m=>m.Cathegory.Id));
Mapper.CreateMap<DebateEditVm, Debate>()
.ForMember(m => m.Cathegory, opt => opt.ResolveUsing(vm => new Cathegory {Id = vm.Cathegory}));
を設定します
私は何が間違っていますか?
あなたの答えはクリス、私はその変更を加えましたが、すべてが更新されたCathegory ...私はやめようとしています! :( – MRFerocius
実際には、あなたの 'Bind'属性だと思いますが、私が言ったように、とにかく全く無意味です。しかし、もしあなたがそれを使うつもりなら、あなたはすべての単一のプロパティについて明示しなければなりません。 .Id'だけでなく、そのリスト内の 'Category'など。 –
私はBind属性を削除して変更しませんでした。 – MRFerocius