【问题标题】:TempData currently unable to handle this requestTempData 目前无法处理此请求
【发布时间】: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


【解决方案1】:

500 Errors 总是带有异常细节,它应该告诉你哪里出了问题!因此,请检查异常/内部异常以了解您的代码崩溃的原因。我还建议您使用 Visual Studio 断点并检查 C# 表达式(您的代码)以查看它是否具有预期的值。

关于 TempData,在 Asp.Net core 中,不能在 TempData 中传递复杂类型。您可以传递简单的类型,例如 stringintGuid 等。这是设计使然。

如果你绝对想在两个动作方法之间传递一个复杂类型的对象,你有几个选择

1) 将您的对象序列化为字符串并将其传递。

var booksString = Newtonsoft.Json.JsonConvert.SerializeObject(books);
TempData["books"] = booksString;
return RedirectToAction("Entry");

在您的 Entry 操作中,您将从 TempData 读取并将字符串反序列化回您的对象。

public IActionResult Entry()
{
    var tempDataStr = TempData["books"] as string;
    // De serialize the string to object
    var books = Newtonsoft.Json.JsonConvert.DeserializeObject<Books>(tempDataStr);
    // Use books
    // to do : return something
}

2) 持久化数据并再次读取。 您可以将数据存储在某处并再次读取。这里有 2 个可供考虑的选项。

  • 会话状态
  • 数据库表

【讨论】:

  • 您问题的最佳答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-20
  • 2016-09-01
  • 2016-07-11
  • 2017-05-03
  • 2020-09-16
  • 2021-11-16
相关资源
最近更新 更多