Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirectToAction results in null model

Tags:

asp.net-mvc

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.

like image 284
Maslow Avatar asked Nov 06 '22 14:11

Maslow


1 Answers

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.

like image 118
Bradley Mountford Avatar answered Nov 15 '22 06:11

Bradley Mountford