MVC3 将消息从一个控制器传递到另一个视图和/或控制器

发布于 2025-01-04 05:29:38 字数 196 浏览 1 评论 0原文

我有一个 MVC3 C#.Net Web 应用程序。具有从 Excel 导入功能。此功能在速率控制器的导入方法中执行。它可以在“速率/导入”视图中查看。导入时,有些行成功,有些行不成功。我将错误收集到 string[] 数组中。如果成功导入一行或多行,则导入被视为成功,因此应用程序将导航回“提案编辑”视图。我想将信息错误传递回提案编辑视图以进行显示。任何IDE如何做到这一点?

I have an MVC3 C#.Net web app. that has an import from Excel function. This function is performed in the Import method of the Rate controller. It is viewed in the Rate/Import view. When importing, some rows are successful, some are not. I am gathering the errors into a string[] array. The import is considered successful if one or more rows is successfully imported, therefore the app navigates back to the Proposal Edit view. I want to pass the informational errors back to the Proposal Edit view for display. Any ides how to do this?

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

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

发布评论

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

评论(1

森林很绿却致人迷途 2025-01-11 05:29:38

有几种方法。

TempData

这在您的控制器中可用。 TempData 在单个重定向中持续存在。

public ActionResult Process()
{
    // ... Process your rows, get array of errors back ...

    TempData["errors"] = errors;
    return RedirectToAction("Edit");
}

public ActionResult Edit()
{
    var errors = (IEnumerable<string>)TempData["errors"];  // Get the errors back.
    return View(errors);  // Pass into the view
}

ModelState

您可以将这些错误直接添加到 ModelState,然后重新显示编辑表单。它不会在重定向后持续存在。

public ActionResult Process()
{
    // ... Process your rows, get array of errors back ...

    for(var i = 0; i < errors.Length; i++)
    {
        ModelState.AddModelError("row" + i, errors[i]);
    }

    // Can't redirect here - ModelState doesn't persist.
    return View("Edit");
}

然后,在编辑视图中,仅显示验证摘要:

@Html.ValidationSummary("The following row errors occured:");

这将呈现此 html。该类是 MVC 设置的默认类。

<div class="validation-summary-errors">
    <span>The following row errors occured:</span>
    <ul>
        <li>...Error 1...</li>
        ... Other errors ...
    </ul>
</div>

There are several ways.

TempData

This is available in your controller. TempData persists across a single redirect.

public ActionResult Process()
{
    // ... Process your rows, get array of errors back ...

    TempData["errors"] = errors;
    return RedirectToAction("Edit");
}

public ActionResult Edit()
{
    var errors = (IEnumerable<string>)TempData["errors"];  // Get the errors back.
    return View(errors);  // Pass into the view
}

ModelState

You could add these errors directly to the ModelState and then redisplay the edit form. It doesn't persist across a redirect.

public ActionResult Process()
{
    // ... Process your rows, get array of errors back ...

    for(var i = 0; i < errors.Length; i++)
    {
        ModelState.AddModelError("row" + i, errors[i]);
    }

    // Can't redirect here - ModelState doesn't persist.
    return View("Edit");
}

Then, in your edit view, just display the validation summary:

@Html.ValidationSummary("The following row errors occured:");

This will render this html. The class is the default one set by MVC.

<div class="validation-summary-errors">
    <span>The following row errors occured:</span>
    <ul>
        <li>...Error 1...</li>
        ... Other errors ...
    </ul>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文