MVC下拉回传问题 [英] MVC dropdown postback issue
问题描述
这是一个previous问题的延续。
<一个href=\"http://stackoverflow.com/questions/27595715/mvc-dropdown-using-viewmodels-without-magic-string/27595950#27595950\">MVC使用的ViewModels没有魔术字符串下拉
随着史蒂芬的帮助下,我设法使创建方法的工作,现在我需要的编辑方法帮助。
我设法得到获取编辑()方法去,但是当我回来后它不保存下拉的新值。我使用调试器,我不明白为什么。它看起来就像是做什么是假设。
// GET:父母/编辑/ 5
公众的ActionResult编辑(INT?ID)
{
如果(ID == NULL)
{
返回新的HTTPStatus codeResult(的HTTPStatus code.BadRequest);
} 父父= db.Parents.Find(ID); 如果(家长== NULL)
{
返回HttpNotFound();
} ParentVM视图模型=新ParentVM()
{
CourtList =新的SelectList(db.CourtsCourtId,CourtName),
SelectedCourt = parent.Court.CourtId,
PARENTID = parent.ParentID,
名字= parent.FirstName,
姓氏= parent.LastName,
儿童= parent.Childs.Select(C =&gt;新建ChildVM()
{
childID的= c.ChildID,
PARENTID = c.ParentID,
名称= c.Name,
DOB = c.DOB,
地址= c.Address
})。了ToList(), }; 返回视图(视图模型); } // POST:父母/编辑/ 5
//从overposting攻击保护,请启用要绑定到特定的属性,
//更多详情请参阅http://go.microsoft.com/fwlink/?LinkId=317598。
[HttpPost]
[ValidateAntiForgeryToken]
公众的ActionResult编辑(ParentVM视图模型)
{
如果(ModelState.IsValid)
{ 法院法院= db.Courts.Find(viewModel.SelectedCourt);
VAR父=新的父()
{
名字= viewModel.FirstName,
姓氏= viewModel.LastName,
PARENTID = viewModel.ParentID,
法院=法庭 }; db.Entry(父).STATE = EntityState.Modified; 的foreach(在viewModel.Children ChildVM项)
{ VAR孩子=新的儿童()
{
名称= item.Name,
DOB = item.DOB,
地址= item.Address,
PARENTID = viewModel.ParentID,
childID的= item.ChildID
};
db.Entry(子).STATE = child.ChildID == 0?
EntityState.Added:
EntityState.Modified; } VAR findChild = db.Childs.Where(X =&GT; x.ParentID == viewModel.ParentID).ToList(); 的foreach(在findChild VAR项)
{ VAR DeleteChild,则= viewModel.Children.Where(X =&GT; x.ChildID == item.ChildID).SingleOrDefault(); 如果(DeleteChild,则== NULL)
{
db.Childs.Remove(项目);
} } db.SaveChanges();
返回RedirectToAction(「指数」);
} viewModel.CourtList =新的SelectList(db.CourtsCourtId,CourtName);
返回视图(视图模型);
}
请尝试这个code。如果您有任何问题,只是问。
看看我的意见,code内。
[HttpPost]
[ValidateAntiForgeryToken]
公众的ActionResult编辑(ParentVM视图模型)
{
如果(ModelState.IsValid)
{
法院法院= db.Courts.Find(viewModel.SelectedCourt);
VAR父= db.Parents.FirstOrDefault(X =&GT; x.ParentID == viewModel.ParentID);
如果(父!= NULL)
{
//父存在于DB - &GT;您只需更新
parent.FirstName = viewModel.FirstName;
parent.LastName =视图。
Model.LastName;
parent.ParentID = viewModel.ParentID;
parent.Court =法院;
}
其他
{
//父不存在DB - &GT;你必须添加一个新的父
db.Parents.Add(新父()
{
名字= viewModel.FirstName,
姓氏= viewModel.LastName,
PARENTID = viewModel.ParentID,
法院=法庭
});
} 的foreach(在viewModel.Children ChildVM项)
{
//在这里,像你这样跟你的父母bevore你可以做同样的
//为DB你的孩子第一次搜索
VAR孩子= db.Childs.FirstOrDefault(X =&GT; x.ChildID == item.ChildID);
如果(孩子!= NULL)
{
child.Name = item.Name;
child.DOB = item.DOB;
child.Address = item.Address;
child.ParentID = viewModel.ParentID;
child.ChildID = item.ChildID;
}
其他
{
db.Childs.Add(新儿童()
{
名称= item.Name,
DOB = item.DOB,
地址= item.Address,
PARENTID = viewModel.ParentID,
childID的= item.ChildID
});
}
} //这里我不明白你想要做什么?
//你能解释一下我吗?
VAR findChild = db.Childs.Where(X =&GT; x.ParentID == viewModel.ParentID).ToList(); 的foreach(在findChild VAR项)
{
VAR DeleteChild,则= viewModel.Children.Where(X =&GT; x.ChildID == item.ChildID).SingleOrDefault(); 如果(DeleteChild,则== NULL)
{
db.Childs.Remove(项目);
}
} db.SaveChanges();
返回RedirectToAction(「指数」);
} viewModel.CourtList =新的SelectList(db.CourtsCourtId,CourtName);
返回视图(视图模型);
}
This is a continuation of a previous questions.
MVC Dropdown using ViewModels without Magic String
With the help of Stephen, I manage to make the Create methods work, now I need help with the Edit methods.
I manage to get the Get Edit () method going but when I post back it does not save the new value of the dropdown. I use the debugger and I could not see why. It looked like it was doing what it was suppose to.
// GET: Parents/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Parent parent = db.Parents.Find(id);
if (parent == null)
{
return HttpNotFound();
}
ParentVM viewModel = new ParentVM()
{
CourtList = new SelectList(db.Courts, "CourtId", "CourtName"),
SelectedCourt = parent.Court.CourtId,
ParentID = parent.ParentID,
FirstName = parent.FirstName,
LastName = parent.LastName,
Children = parent.Childs.Select(c => new ChildVM()
{
ChildID = c.ChildID,
ParentID = c.ParentID,
Name = c.Name,
DOB = c.DOB,
Address = c.Address
}).ToList(),
};
return View(viewModel);
}
// POST: Parents/Edit/5
// 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 Edit(ParentVM viewModel)
{
if (ModelState.IsValid)
{
Court court = db.Courts.Find(viewModel.SelectedCourt);
var parent = new Parent()
{
FirstName = viewModel.FirstName,
LastName = viewModel.LastName,
ParentID = viewModel.ParentID,
Court = court
};
db.Entry(parent).State = EntityState.Modified;
foreach (ChildVM item in viewModel.Children)
{
var child = new Child()
{
Name = item.Name,
DOB = item.DOB,
Address = item.Address,
ParentID = viewModel.ParentID,
ChildID = item.ChildID
};
db.Entry(child).State = child.ChildID == 0 ?
EntityState.Added :
EntityState.Modified;
}
var findChild = db.Childs.Where(x => x.ParentID == viewModel.ParentID).ToList();
foreach (var item in findChild)
{
var deleteChild = viewModel.Children.Where(x => x.ChildID == item.ChildID).SingleOrDefault();
if (deleteChild == null)
{
db.Childs.Remove(item);
}
}
db.SaveChanges();
return RedirectToAction("Index");
}
viewModel.CourtList = new SelectList(db.Courts, "CourtId", "CourtName");
return View(viewModel);
}
Please try out this code. If you have any questions just ask.
Check out my comments inside the code.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(ParentVM viewModel)
{
if (ModelState.IsValid)
{
Court court = db.Courts.Find(viewModel.SelectedCourt);
var parent = db.Parents.FirstOrDefault(x => x.ParentID == viewModel.ParentID);
if (parent != null)
{
// Parent exists in DB --> You can just update it
parent.FirstName = viewModel.FirstName;
parent.LastName = view;
Model.LastName;
parent.ParentID = viewModel.ParentID;
parent.Court = court;
}
else
{
// Parent does not exist in DB --> You have to Add a new Parent
db.Parents.Add(new Parent()
{
FirstName = viewModel.FirstName,
LastName = viewModel.LastName,
ParentID = viewModel.ParentID,
Court = court
});
}
foreach (ChildVM item in viewModel.Children)
{
// Here you can do exactly the same like you did bevore with your Parents
// First search for your Child in DB
var child = db.Childs.FirstOrDefault(x => x.ChildID == item.ChildID);
if (child != null)
{
child.Name = item.Name;
child.DOB = item.DOB;
child.Address = item.Address;
child.ParentID = viewModel.ParentID;
child.ChildID = item.ChildID;
}
else
{
db.Childs.Add(new Child()
{
Name = item.Name,
DOB = item.DOB,
Address = item.Address,
ParentID = viewModel.ParentID,
ChildID = item.ChildID
});
}
}
// Here I don't get what you want to do...
// Can you explain me that?
var findChild = db.Childs.Where(x => x.ParentID == viewModel.ParentID).ToList();
foreach (var item in findChild)
{
var deleteChild = viewModel.Children.Where(x => x.ChildID == item.ChildID).SingleOrDefault();
if (deleteChild == null)
{
db.Childs.Remove(item);
}
}
db.SaveChanges();
return RedirectToAction("Index");
}
viewModel.CourtList = new SelectList(db.Courts, "CourtId", "CourtName");
return View(viewModel);
}
这篇关于MVC下拉回传问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!