我对 Controller 有 2 个操作:
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);
}
}
当调用 Index 方法时,我得到一个空模型。当 Accounting 方法通过它的 url 直接调用时,我得到一个水合模型。
最佳答案
这与其说是一种解决方法,不如说是一种答案。我不知道为什么你会得到一个空模型,因为它看起来应该可以工作。事实上,当我自己尝试时,我可以确认您看到的行为。 [编辑:我在我的初始测试代码中发现了一个导致我自己的空模型的缺陷。现在已经更正了,我的测试使用 RedirectToAction 可以正常工作。] 如果有一个原因,我不知道我的头顶。
现在是解决方法...我假设您是这样做的,因为默认路由将所有流量发送到 http://www.domain.com/Calculators到“索引”。那么为什么不创建一个这样的新路由:
routes.MapRoute(
"Accounting",
"Calculators/{action}/",
new { controller = "Calculators", action = "Accounting" }
);
此路由指定 Calculators Controller 的默认操作将是“Accounting”而不是 Index。
关于asp.net-mvc - redirectToAction 导致空模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2526259/