【发布时间】:2014-11-03 15:13:01
【问题描述】:
我需要为 HttpVerb 使用相同的 ViewBag 对象,它们是 HttpGet 和 HttpPost。因此我不想声明 ViewBags 两次。我创建了一个参数方法,并且每当我想使用它时都会调用这个方法遵循示例。是正确的方法还是您对此问题有任何解决方案?
public ActionResult Index()
{
SetViewObjects(null);
return View();
}
[HttpPost]
public ActionResult Index(Person model)
{
SetViewObjects(model.PersonId);
return View(model);
}
public void SetViewObjects(short? personId)
{
IEnumerable<Person> person = null;
if(personId.HasValue)
{
person = db.GetPerson().Where(m=>m.PersonId == personId);
}
else
{
person = db.GetPerson();
}
ViewBag.Person = person;
}
【问题讨论】:
标签: asp.net asp.net-mvc viewbag httpverbs