を示す誤動テーブルに基づく意見
-
26-09-2019 - |
質問
私の見たい動作を実現には各行はテーブルと同様のこと、 ~/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小形式のページです。このアプローチが大きくか この 記事などを取り扱いに誤りをしたいです。
他のテイ?
解決力を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) %>
<% } %>
よろしく!
解決
試してみては...
があり 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");
}
のTempDataメッセージのみのご利用が可能に 現在のリクエストに対 まった後もリフレッシュ。
でもさらに調整するこの方向になるようなエラー報告は、ユーザー/おります。
他のヒント
やってみます:
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ます。