【问题标题】:showing errors from actions in table-based views在基于表格的视图中显示来自操作的错误
【发布时间】:2010-06-11 14:36:29
【问题描述】:

我有一个视图,我想对表中每一行中的项目执行不同的操作,类似于此(例如,~/Views/Thing/Manage.aspx):

<table>
  <% foreach (thing in Model) { %>
    <tr>
      <td><%: thing.x %></td>
      <td>
        <% using (Html.BeginForm("SetEnabled", "Thing")) { %> 
          <%: Html.Hidden("x", thing.x) %>
          <%: Html.Hidden("enable", !thing.Enabled) %>
          <input type="submit"  
                 value="<%: thing.Enabled ? "Disable" : "Enable" %>" />
        <% } %>
      </td>    
      <!-- more tds with similar action forms here, a few per table row -->     
   </tr>
  <% } %>

在我的ThingController 中,我有类似以下的功能:

public ActionResult Manage() {
  return View(ThingService.GetThings());
}

[HttpPost]
public ActionResult SetEnabled(string x, bool enable) {
  try {
    ThingService.SetEnabled(x, enable);
  } catch (Exception ex) {
    ModelState.AddModelError("", ex.Message); // I know this is wrong...
  }
  return RedirectToAction("Manage");
}

在大多数情况下,这工作正常。问题是如果ThingService.SetEnabled 抛出错误,我希望能够在表格顶部显示错误。我在页面中使用Html.ValidationSummary() 尝试了一些操作,但无法正常工作。

请注意,我不想将用户发送到单独的页面来执行此操作,并且我正在尝试在不使用任何 javascript 的情况下执行此操作。

我打算以最好的方式展示我的桌子吗?如何以我希望的方式显示错误?我最终会在页面上得到大约 40 个小表格。这种方法主要来自this 文章,但它并没有以我需要的方式处理错误。

有接受者吗?


感谢@Shaharyar:

public ActionResult Manage() {
  if (TempData["Error"] != null)
    ModelState.AddModelError("", TempData["Error"] as string);
  return View(ThingService.GetThings());
}

[HttpPost]
public ActionResult SetEnabled(string x, bool enable) {
  try {
    ThingService.SetEnabled(x, enable);
  } catch (Exception ex) {
    TempData["Error"] = ex.Message;
  }
  return RedirectToAction("Manage");
}

然后是我表格顶部的 ValidationSummary 的一个小表单。

<% using (Html.BeginForm()) { %>
  <%: Html.ValidationSummary(false) %>
<% } %>

谢谢!

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-2 asp.net-4.0


    【解决方案1】:

    让我们试试……

    有一个TempData 字典可供您执行此类操作。

    您可能需要依赖另一个页面来为您处理错误。

    因为一旦无法将 ViewModel 传递给 View 就会抛出异常。

    但是如果模型有一些问题,你可以这样做(只需将一个空模型传递给视图):

    public SetEnabled(string x, bool enable) {
      try {
        ThingService.SetEnabled(x, enable);
        return View(viewModel);
      } catch {
        TempData["GetThingsError"] = "Oops! Some error happened while processing your request!"
        return View(); 
        /*
         * Note that you can also pass the initial model
         * back to the view -> this will do the model validation 
         * (specified in your model) 
         */
      }
      return RedirectToAction("Manage");
    }
    

    TempData 消息仅适用于 current request,刷新后将消失。

    可能需要进一步调整,但这将是向用户/客户报告此类错误的方向。

    【讨论】:

    • 谢谢,解决了。理想的是它仅适用于当前请求。我已将我使用的代码添加到单独的回复中。
    • 很高兴它帮助了你!您也可以编辑您的原始问题并在解决方案中添加一个新部分。 (StackOverflow 上的首选行为);-)
    【解决方案2】:

    尝试做:

     try {
        ThingService.SetEnabled(x, enable);
      } catch (Exception ex) {
        ModelState.AddModelError("", ex.Message); // I know this is wrong...
        return View(); //return to the view to display the error
      }
    

    如果您返回错误的同一个视图,它会重新加载该视图;您可能需要重新加载一些数据项,但返回错误的视图时,框架应从 ModelState 中提取这些错误并显示它们。

    最有效的方法是使用 JQuery 将表单提交到服务器,这样您就不必总是重新加载页面,并在客户端显示一条消息。

    HTH。

    【讨论】:

    • 没有 SetEnabled 视图,它仅用于发布。我想在发布到 SetEnabled 后显示带有错误的管理视图。另外,我想在没有 javascript 的情况下做到这一点。
    • 好的,尝试返回 View("Manage");只要模型状态有错误,就应该重新加载视图的状态...
    猜你喜欢
    • 2021-12-15
    • 2018-07-03
    • 2014-05-05
    • 2020-03-28
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 2011-07-19
    相关资源
    最近更新 更多