【发布时间】:2015-10-07 08:33:19
【问题描述】:
// GET: /Winches/Edit/5
public async Task<ActionResult> Edit(int? id)
{
WinchesBrand winchesbrand = await db.WinchesBrands.FindAsync(id);
var model = new WinchModel
{
WinchBrandId = winchesbrand.WinchBrandId,
WinchBrandName = winchesbrand.WinchBrandName,
RopeList = new List<int?>() { }
};
foreach (var rope in winchesbrand.Ropes)
{
model.RopeList.Add(rope.RopeId);
}
if (model.RopeList.Any() == false)
{
model.RopeList.Add(null);
}
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
ViewBag.RopeList = db.Ropes.Where(e => e.IsDeleted == false).ToList();
return View(model);
}
对不起我的英语, 我不知道,如何为这个编辑写帖子
这是我的变种:
[HttpPost]
public async Task<ActionResult> Edit(WinchModel model)
{
if (ModelState.IsValid)
{
List<Rope> ropesList = new List<Rope>();
WinchesBrand winch = new WinchesBrand
{
WinchBrandName = model.WinchBrandName,
Ropes = ropesList
};
//db.WinchesBrands.Where(w => w.WinchBrandName == model.WinchBrandName)
// .Update();
foreach (var ropeId in model.RopeList.Where(w => w > 0))
{
db.Ropes.Find(ropeId).WinchesBrand = winch;
}
if (model.RopeList.Any() == false)
{
model.RopeList.Add(null);
}
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
ViewBag.RopeList = new SelectList(db.Ropes.Where(e => e.IsDeleted == false), "RopeId", "RopeName");
return View(model);
}
但这不刷新(我不知道这个命令)
{db.WinchesBrands.Where(w => w.WinchBrandName == model.WinchBrandName)
// .Update();}
PC我刚开始学习这个
【问题讨论】:
标签: c# model-view-controller controller