通过RedirectToAction传递TempData [英] Passing TempData with RedirectToAction
问题描述
简介: 我是试图学习ASP.NET Core MVC的.NET学生.所以请谅解.我已经在网上搜索了我的问题的答案,但还没有找到适合我的解决方案.
Intro: I am a .NET studet trying to learn ASP.NET Core MVC. So please be understanding. I have searched the web for an answer to my problem, but havent found a solution that works for me.
问题: 我想在创建帖子时将验证消息从我的create post方法传递到索引IActionmethod,并且它们现在将其显示为警报消息.我已经在网络上阅读到ViewBag主体在重定向中幸存下来,但是TempData确实可以.到目前为止,这是我的代码.
Problem: I want to pass a validation message from my create post method to the index IActionmethod whenever a post has been created and them show it as an alert message for now. I have read on the web that ViewBag dosent survive a redirect, but a TempData does. This is my code so far.
创建发布方法:
public IActionResult CreatePost(string textContent, string headline, string type)
{
var catType = new Category() { CategoryType = type.ToUpper() };
if (db.Category.Any(s => s.CategoryType.Trim().ToLower() == type.Trim().ToLower()))
catType = db.Category.FirstOrDefault(s => s.CategoryType.Trim().ToLower() == type.Trim().ToLower());
var newPost = new Post()
{
Content = textContent,
Header = headline,
DateOfPost = DateTime.Now,
category = catType
};
db.Posts.Add(newPost);
db.SaveChanges();
TempData["validation"] = "Your post hase been publsihed";
return RedirectToAction("Index");
}
索引方法:
public IActionResult Index()
{
var validation = TempData["validation"];
var posts = (from x in db.Posts
orderby x.DateOfPost descending
orderby x.PostID descending
select x);
return View(posts);
}
我已经尝试过以下指南: ClickThis 和该版本: ClickThis2 ,但我收到了以下消息:
I have tried this guide: ClickThis and this one: ClickThis2 but I got this message:
我知道Gudie 2号的这一行可能很重要,但现在没有如何应用它. -
I know this line from gudie number 2 might be important, but didnt now how to apply it. -
var product = TempData["myTempData"] as Product;
我最后要做的就是将其传递给索引视图,但是不知道如何操作.我目前正在通过索引传递模型.
The last thing I want to do is pass it to the index view, but dont know how. I am currently passing a model from the index.
告诉我您是否想看的更多内容.就像依赖.
Tell me if it is anything more you would like to see. Like dependencies.
我得到的所有帮助都是金子,将不胜感激!!!
All the help I get is gold and will be much appreciate!!!
推荐答案
您是否配置了会话? TempData在后台使用会话.
Did you configure Session? TempData is using session behind the scenes.
Project.json
Project.json
"Microsoft.AspNetCore.Session": "1.1.0"
这是Startup.cs文件. -ConfigureServices
方法
Here is the Startup.cs file. - ConfigureServices
method
public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
services.AddSession();
services.AddMvc();
}
和Configure
方法.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseSession();
app.UseMvc(routes => {
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
现在尝试使用TempData,它将起作用.
Now try with TempData, it will work.
您可以使用set ASPNETCORE_ENVIRONMENT=Development
环境变量设置环境.
And you can set the environment with set ASPNETCORE_ENVIRONMENT=Development
environment variable.
这篇关于通过RedirectToAction传递TempData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!