而不是:
public ActionResult Index()
{
ViewData["foo"] = _repository.GetFoos().ToList();
ViewData["bar"] = new string[] { "blah" };
ViewData["baz"] = "";
return View();
}
然后:
public ActionResult Index(string baz)
{
// Do stuff...
ViewData["foo"] = _repository.GetFoos().ToList();
ViewData["bar"] = new string[] { "blah" };
ViewData["baz"] = baz;
return View();
}
我在考虑如何减少打字错误,使viewdata更结构化一点,而不必添加另一个viewModel类。我想到了以下几点:
public ActionResult Index()
{
var foo = _repository.GetFoos().ToList();
var bar = new string[] { "blah" };
var baz = null;
ViewData = new ViewDataDictionary {
{"foo", foo},
{"bar", bar},
{"baz", baz }
};
return View();
}
public ActionResult Index(string baz)
{
var foo = _repository.GetFoos().ToList();
var bar = new string[] { "blah" };
var baz = null;
// Do Stuff...
ViewData = new ViewDataDictionary {
{"foo", foo},
{"bar", bar},
{"baz", baz}
};
return View();
}
这样覆盖视图数据是个坏主意吗?我很确定这不会导致渲染部分出现问题,但我是否不再能够在视图中调用渲染?