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. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc-3 – 在App_code文件夹中使用razor @helper使用
- 如何减少IISRESET在IIS 5.1中开发ASP.NET Web应用程序的需求
- asp.net – 提高gridview的分页性能?
- asp.net-mvc – 如何从ASP.NET MVC VIEWS文件夹访问HTML文件
- 身份验证 – ASP:使用JMail进行SMTP身份验证失败
- 只需发布已修改的控制器,模型和ASP.NET MVC项目的某些部分
- asp.net-mvc-3 – ASP.NET MVC – 防止Ajax.ActionLinks上的
- asp.net-mvc – asp.net mvc wiki
- asp.net-mvc – 使用IoC在Controller中注入HttpContextBase
- asp.net – 如何从搜索引擎中完全隐藏网站?