显示基于表的视图中的操作错误
我有一个视图,我想对表中每一行中的项目执行不同的操作,类似于此(例如,~/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>
<% } %>
在我的 < code>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 个小表格。这种方法主要来自 这篇文章,但它没有按照我需要的方式处理错误。
有接受者吗?
感谢@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) %>
<% } %>
谢谢!
I have a view where I want to perform different actions on the items in each row in a table, similar to this (in, say, ~/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>
<% } %>
In my ThingController
, I have functions similar to the following:
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");
}
In the most part, this is working fine. The problem is that if ThingService.SetEnabled
throws an error, I want to be able to display the error at the top of the table. I've tried a few things with Html.ValidationSummary()
in the page but I can't get it to work.
Note that I don't want to send the user to a separate page to do this, and I'm trying to do it without using any javascript.
Am I going about displaying my table in the best way? How do I get the errors displayed in the way I want them to? I will end up with perhaps 40 small forms on the page. This approach comes largely from this article, but it doesn't handle the errors in the way I need to.
Any takers?
Solved thanks to @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");
}
Then just a small form for the ValidationSummary at the top of my table.
<% using (Html.BeginForm()) { %>
<%: Html.ValidationSummary(false) %>
<% } %>
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我们尝试一下...
有一个
TempData
字典可供您执行此类操作。您可能不得不依赖另一个页面来为您处理错误。
因为一旦ViewModel无法传递给View就会抛出异常。
但是,如果模型有一些问题,您可以执行以下操作(只需将空模型传递给视图):
TempData 消息仅适用于 当前请求并且将刷新后就消失了。
它可能需要一些进一步的调整,但这将是向用户/客户进行此类错误报告的方向。
Let's try...
There is a
TempData
dictionary available for you to do this kind of things.And you might have to rely on another page to handle the errors for you.
Because an exception will be thrown as soon as the ViewModel can't be passed to the View.
But if the model has some problems, you could do the following (just pass an empty model to the view):
The TempData message is only available for the current request and will be gone after a refresh.
It might need some further tweaking but that would be the direction to do that kind of error reporting to the user/customer.
尝试这样做:
如果您返回错误的同一视图,它会重新加载该视图;您可能需要重新加载一些数据项,但返回错误的视图后,框架应该从 ModelState 中提取这些错误并显示它们。
最有效的方法是使用 JQuery 将表单提交到服务器,这样您就不必总是重新加载页面并在客户端上显示消息。
HTH。
Try doing:
If you return the same view that's in error, it reloads the view; there are some data items you may need to reload, but returning the view in error the framework should then extract those errors from ModelState and display them.
The most efficient way is to use JQuery to submit the form to the server, so you aren't always reloading the page, and display a message on the client.
HTH.