如何通过 RedirectToAction 传递类

发布于 2025-01-07 13:49:01 字数 579 浏览 2 评论 0原文

我有以下代码:

    public ActionResult Index()
    {
        AdminPreRegUploadModel model = new AdminPreRegUploadModel()
        {
            SuccessCount = successAddedCount,
            FailureCount = failedAddedCount,
            AddedFailure = addedFailure,
            AddedSuccess = addedSuccess
        };
        return RedirectToAction("PreRegExceUpload", new { model = model });
    }

    public ActionResult PreRegExceUpload(AdminPreRegUploadModel model)
    {
        return View(model);
    }

但是当我在 PreRegExcelUpload 上断点时模型为空。为什么?

I have the following code:

    public ActionResult Index()
    {
        AdminPreRegUploadModel model = new AdminPreRegUploadModel()
        {
            SuccessCount = successAddedCount,
            FailureCount = failedAddedCount,
            AddedFailure = addedFailure,
            AddedSuccess = addedSuccess
        };
        return RedirectToAction("PreRegExceUpload", new { model = model });
    }

    public ActionResult PreRegExceUpload(AdminPreRegUploadModel model)
    {
        return View(model);
    }

but model is null when I breakpoint on PreRegExcelUpload. Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

音盲 2025-01-14 13:49:02

我建议使用 TempData,而不是在 Evgeny Levin 的答案中使用 Session 对象。请参阅 http://有关 TempData 的信息,请访问 rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications

您还可以通过在 Index 函数中调用 return PreRegExceUpload(model); 而不是 return RedirectToAction(..) 来解决此问题。

Instead of using the Session object in Evgeny Levin's answer I would suggest to use TempData. See http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications about TempData.

You could also fix this by calling return PreRegExceUpload(model); instead of return RedirectToAction(..) in you Index function.

注定孤独终老 2025-01-14 13:49:02

TempData 只是 Session 的“智能”包装器,在幕后它的行为方式仍然相同。

因为它只有 4 个字段,所以我将通过查询字符串传递它们。

尽可能避免会话/临时数据,在这种情况下确实如此。

你确定这是你的完整代码吗?因为它没有意义。

如果您发布一些数据并将其保存到数据库(例如),通常您会重定向到传递唯一标识符(通常在保存后生成)的另一个操作,从数据库中取回它并返回视图。

这是更好的做法。

如果您多解释一下您的场景,并显示您使用的正确代码,我可以提供进一步帮助。

TempData is just a "smart" wrapper for the Session, under the hood it still acts the same way.

Since it's only 4 fields, i would pass them via querystring.

Always try and avoid session/tempdata where possible, for which in this scenario it certainly is.

Are you sure that's your full code? As it doesn't make sense.

If your POST'ing some data and saving it to the database (for example), usually you redirect to another action passing the unique identifier (which is usually generated after the save), fetch it back from the DB and return the view.

That is much better practice.

If you explain your scenario a bit more, and show the proper code your using, i can help further.

灯下孤影 2025-01-14 13:49:02

使用会话将模型传递给方法:

public ActionResult Index()
{
    AdminPreRegUploadModel model = new AdminPreRegUploadModel()
    {
        SuccessCount = successAddedCount,
        FailureCount = failedAddedCount,
        AddedFailure = addedFailure,
        AddedSuccess = addedSuccess
    };
    Session["someKey"] = model;
    return RedirectToAction("PreRegExceUpload");
}

public ActionResult PreRegExceUpload()
{
    var model = (AdminPreRegUploadModel) Session["someKey"];
    Session["someKey"] = null;
    return View(model);
}

方法 RedirectToAction() 不能采用非原始类型作为参数,因为 url 参数是字符串。

Use session to pass model to method:

public ActionResult Index()
{
    AdminPreRegUploadModel model = new AdminPreRegUploadModel()
    {
        SuccessCount = successAddedCount,
        FailureCount = failedAddedCount,
        AddedFailure = addedFailure,
        AddedSuccess = addedSuccess
    };
    Session["someKey"] = model;
    return RedirectToAction("PreRegExceUpload");
}

public ActionResult PreRegExceUpload()
{
    var model = (AdminPreRegUploadModel) Session["someKey"];
    Session["someKey"] = null;
    return View(model);
}

Method RedirectToAction() can't take non primitive types as parameters, because url parameters is string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文