asp.net-mvc – 在操作之间传递参数
发布时间:2020-12-16 06:47:15 所属栏目:asp.Net 来源:网络整理
导读:我试过这个: public ActionResult Index() // it starts here{ return RedirectToAction("ind",new { name = "aaaaaaa" });}[ActionName("ind")]public ActionResult Index(string name)// here,name is 'aaaaaaa'{ return View();} 它的工作原理.. 所以,我
我试过这个:
public ActionResult Index() // << it starts here { return RedirectToAction("ind",new { name = "aaaaaaa" }); } [ActionName("ind")] public ActionResult Index(string name)// here,name is 'aaaaaaa' { return View(); } 它的工作原理.. 所以,我试过这个: [HttpPost] public ActionResult Search(string cnpj) // starts here { List<Client> Client = db.Client // it always find one client .Where(c => cnpj.Equals(c.Cnpj)) .ToList(); return RedirectToAction("Index",Client); // client is not null } public ActionResult Index(List<Client> Client) //but when goes here,client is always null { if (Client != null) return View(Client); return View(db.Client.ToList()); } 为什么会这样?第二个代码块有问题吗? 解决方法
您只能在重定向中传递基本类型,您可以将TempData用于复杂类型.
[HttpPost] public ActionResult Search(string cnpj) // starts here { List<Client> Client = db.Client // it always find one client .Where(c => cnpj.Equals(c.Cnpj)) .ToList(); TempData["client"] = Client; //<================= return RedirectToAction("Index"); } public ActionResult Index() { var Client = TempData["client"]; //<================= if (Client != null) return View(Client); return View(db.Client.ToList()); } 基本上,TempData就像在Session中保存数据一样,但数据将在请求结束时自动删除. TempData on MSDN 笔记: > C#中常见的命名约定将私有变量定义为驼峰式.客户而不是客户.>对于List< Client>变量我会使用客户端作为名称而不是客户端.>您应该将资源用于“客户端”字符串,以便它不会失去同步,这意味着一种方法将数据放入“客户端”,而另一种方法在“客户端”或“客户端数据”中查找数据 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 从标签容器中的数据绑定gridview丢失更新面板触
- asp.net中的泛型处理程序是什么?
- asp.net-mvc-3 – 是否需要StructureMap HttpContextScoped
- asp.net-mvc – 如何使用具有可空类型的强类型HTML帮助程序
- asp.net – RequiredFieldValidator必须单击两次
- asp.net – Internet Explorer 11在服务器端的检测
- asp.net – 如何在.NET中调用服务器端事件
- 实现ASP.NET多文件上传程序代码
- asp.net – 在IIS7中重启网站以“刷新”Castle Windsor配置
- 当值不为null时,清除在ASP.NET MVC视图中输出值的方法
推荐文章
站长推荐
- asp.net-mvc – Web Essentials浏览器链接在Visu
- 为什么ASP.Net重写runat = server锚控件的相对路
- asp.net – 如何防止网页上的CSS缓存?
- asp.net – UpdatePanel和ClientIDMode =“静态”
- asp.net-mvc – ASP.NET MVC 3 – ViewModel最佳
- asp.net-mvc – 为什么ASP.NET MVC忽略了我的尾随
- asp.net – 如何从HTTP请求获取MAC地址?
- asp.net-mvc – 剃刀替换的优点(@href)
- 使用ASP.NET 5中的默认DI容器一次注册所有服务,类
- asp经典 – ASP Classic中的注释代码
热点阅读