Instead of:
public ActionResult Index()
{
ViewData["foo"] = _repository.GetFoos().ToList();
ViewData["bar"] = new string[] { "blah" };
ViewData["baz"] = "";
return View();
}
and then:
public ActionResult Index(string baz)
{
// Do stuff...
ViewData["foo"] = _repository.GetFoos().ToList();
ViewData["bar"] = new string[] { "blah" };
ViewData["baz"] = baz;
return View();
}
I was thinking about ways to reduce typo errors by making the ViewData a little more structured, without having to add (yet) another ViewModel class. I came up with the following:
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();
}
Is it a bad idea to overwrite the ViewData like that? I'm pretty sure this wouldn't cause problems with RenderPartials, but would I no longer be able to call RenderAction in a view?