【发布时间】:2018-08-05 00:19:35
【问题描述】:
如何解决 TempData 无法正常工作,页面重定向到 localhost is currently unable to handle this request.
HTTP ERROR 500
我想要实现的是创建一个接受模型的表单,并且可以添加多个数据,我想将模型的临时列表存储到TempData,然后再将其全部插入数据库,我被这个问题卡住了.我怎么可能解决这个问题?我错过了什么吗?
这是我的Startup.cs 代码:
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//some code here
//services.AddMvc();
services.AddMvc().AddSessionStateTempDataProvider();
services.AddSession();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//some code here
app.UseSession();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
控制器代码:
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create1(BooksViewModel vmodel, Books books)
{
if (ModelState.IsValid)
{
//some code here
TempData["BooksViewModel"] = books;
return RedirectToAction(nameof(Entry));
}
return View();
}
public IActionResult Entry()
{
BooksViewModel vmodel = (BooksViewModel)TempData["BooksViewModel"];
List<BooksViewModel> list = new List<BooksViewModel>();
list = (List<BooksViewModel>)TempData["BooksViewModelList"];
if (vmodel != null)
{
//some code here
}
TempData["BooksViewModelList"] = list;
return View(list);
}
Entry.cshtml 代码:
@model IEnumerable<QnE_Accounting.Models.TransactionsViewModel.BooksViewModel>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
//some code here
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
//some code here
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>
【问题讨论】:
-
您能向我们展示与您的项目相关的视图或存储库吗?
-
@mvermef,已经更新了代码先生
-
@mvermef,我不知道我的视图是否正常工作,因为我陷入了当前的问题
标签: c# asp.net-core asp.net-core-mvc tempdata