加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – redirectToAction导致null模型

发布时间:2020-12-16 03:19:02 所属栏目:asp.Net 来源:网络整理
导读:我在控制器上有2个动作: public class CalculatorsController : Controller{ // // GET: /Calculators/ public ActionResult Index() { return RedirectToAction("Accounting"); } public ActionResult Accounting() { var combatants = Models.Persistence
我在控制器上有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方法时,我会得到一个null模型.当通过它的url直接调用Accounting方法时,我会得到一个水合模型.

解决方法

这不是解决方法而是解决方法.我不知道为什么你得到一个null模型,因为它看起来应该工作.事实上,当我自己尝试时,我可以确认你所看到的行为. [编辑:我在我的初始测试代码中发现了一个导致我自己的null模型的缺陷.现在已经纠正了,我的测试使用RedirectToAction工作正常.]如果有一个原因,我不知道它在我的头顶.

现在为解决方法…我假设你这样做,因为默认路由将所有流量发送到http://www.domain.com/Calculators到“索引”.那么为什么不创建这样的新路线:

routes.MapRoute(
  "Accounting","Calculators/{action}/",new { controller = "Calculators",action = "Accounting" }
);

此路由指定计算器控制器的默认操作将是“Accounting”而不是Index.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读