How can I fix TempData is not working, page redirects to localhost is currently unable to handle this request.
HTTP ERROR 500
What I want to achieve is to create a form that accepts model, and can add multiple data, I want to store the temporary list of model to the TempData
before inserting it all to database and I got stuck from this issue. How can I possibly fix this? Am I missing something?
Here is my Startup.cs
code:
// 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?}");
});
}
Controller code:
[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 code:
@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>