代码之家  ›  专栏  ›  技术社区  ›  enashnash

在基于表的视图中显示操作错误

  •  0
  • enashnash  · 技术社区  · 15 年前

    我有一个视图,希望对表中每一行中的项执行不同的操作,类似于(例如, ~/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() 但我没法用。

    我要用最好的方式展示我的桌子吗?如何以我希望的方式显示错误?最后,我会在页面上列出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) %>
    <% } %>
    

    谢谢!

    2 回复  |  直到 8 年前
        1
  •  0
  •   Faizan S.    15 年前

    让我们试试。。。

    TempData 字典可供你做这类事情。

    因为一旦ViewModel无法传递给视图,就会引发异常。

    但是如果模型有一些问题,可以执行以下操作(只需将空模型传递给视图):

    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");
    }
    

    current request 刷新后就会消失。

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

        2
  •  0
  •   Brian Mains    15 年前

     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将表单提交到服务器,这样就不必总是重新加载页面,并在客户端上显示消息。