I have 2 actions on a controller:
public class CalculatorsController : Controller
{
//
// GET: /Calculators/
public ActionResult Index()
{
return RedirectToAction("Accounting");
}
public ActionResult Accounting()
{
var combatants = Models.Persistence.InMemoryCombatantPersistence.GetCombatants();
Debug.Assert(combatants != null);
var bvm = new BalanceViewModel(combatants);
Debug.Assert(bvm!=null);
Debug.Assert(bvm.Combatants != null);
return View(bvm);
}
}
When the Index method is called, I get a null model coming out. When the Accounting method is called directly via it's url, I get a hydrated model.
This is less an answer than a workaround. I am not sure why you are getting a null model as it looks like it should work. In fact, I can confirm the behavior you are seeing when I try it out myself. [EDIT: I discovered a flaw in my initial test code that was causing my own null model. Now that that is corrected, my test works fine using RedirectToAction.] If there is a reason for it, I don't know it off the top of my head.
Now for the workaround...I assume that you are doing it this way since the default route sends all traffic to http://www.domain.com/Calculators to "Index". So why not create a new route like this:
routes.MapRoute(
"Accounting",
"Calculators/{action}/",
new { controller = "Calculators", action = "Accounting" }
);
This route specifies the default action to the Calculators controller will be "Accounting" instead of Index.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With