我需要为 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;
}