成功消息而不是模型状态错误消息

发布于 2024-08-19 23:08:12 字数 207 浏览 4 评论 0原文

对于错误消息、验证错误等,您有

ModelState.AddErrorMessage("Fool!");

但是,您将成功回复放在哪里,例如“您成功地将很多钱转移给您的前任”。 +“您的余额现在为零”。我仍然想在控制器级别设置它,最好以键值方式设置,与错误消息相同,但不使模型状态无效。

这通常是如何完成的?查看数据?

For error messages, validation faults etc you have

ModelState.AddErrorMessage("Fool!");

But, where do you put success responses like "You successfully transfered alot of money to your ex." + "Your balance is now zero". I still want to set it at the controller level and preferably in key-value way, the same way as errormessages but without invalidating the modelstate.

How is this usually done? ViewData?

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

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

发布评论

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

评论(3

国际总奸 2024-08-26 23:08:12

我将使用我想要在控制器中显示的消息填充 TempData["success"] (或您想要给它的任何键),然后适当地重定向(例如,如果我编辑用户,我会重定向回用户列表) 。这依赖于 POST/Redirect/GET 模式——无论如何,这是一个很好的实践。

TempData["success"] = "Your Balance is now zero";

在母版页中,我有一个部分检查该变量并在样式漂亮的 div 中显示消息。像这样的东西(可能不是100%正确):

<% if(TempData["success"] != null) { %>
      <div id="SuccessMessage"><%= Html.Encode(TempData["success"]) %><div>
<% } %>

I would populate TempData["success"] (or what ever key you want to give it) with the message I want to display within the controller, then redirect appropriately (e.g. if I edit a user, I redirect back to the user list). This relies on POST/Redirect/GET pattern - which is a good practice anyway.

TempData["success"] = "Your Balance is now zero";

In the master page I have a section that checks that variable and displays the message in a nice styled div. Something like (may not be 100% correct):

<% if(TempData["success"] != null) { %>
      <div id="SuccessMessage"><%= Html.Encode(TempData["success"]) %><div>
<% } %>
晚风撩人 2024-08-26 23:08:12

我想您可以检查模型状态并在模型中设置一个变量...

public ActionResult MyAction(MyEntity model)
{
  //Here would be some validation, which returns with ModelState errors

  //Now set the validity of the modelstate as the IsValid property in your entity
  model.IsValid = ModelState.IsValid;

  return View(model);
}

在您看来...

<% if(Model.IsValid) { %>
  <p>You successfully transfered your balance to your ex.</p>
<% } %>

编辑:鉴于您更新的问题,我认为您正在考虑采取错误的方法。我会同意其他答案并遵循 PRG 模式。这绝对比尝试添加假错误更有意义。

I suppose you could check the modelstate and set a variable in your model...

public ActionResult MyAction(MyEntity model)
{
  //Here would be some validation, which returns with ModelState errors

  //Now set the validity of the modelstate as the IsValid property in your entity
  model.IsValid = ModelState.IsValid;

  return View(model);
}

In your view...

<% if(Model.IsValid) { %>
  <p>You successfully transfered your balance to your ex.</p>
<% } %>

Edit: Given your updated question, I think you're looking at taking the wrong approach. I would go along with the other answers and follow a PRG pattern. This definitely makes more sense than trying to add a fake error.

鹊巢 2024-08-26 23:08:12

您应该实现类似 POST/Redirect/GET 模式的内容,并在验证所有验证并且一切正常执行后,在操作方法末尾“重定向”到另一个视图。您可以将整个对象实例传递到目标视图,也可以仅传递纯文本消息,也可以从 web.config 或资源文件中提取目标视图本身中的文本。

例如,我在名为“ChangeSuccess.aspx”的共享文件夹中有一个视图,我将所有成功的编辑和创建重定向到该视图。

你像这样“重定向”

return View("ChangeSuccess", objectInstance);

(注意:实际上并没有重定向,请参阅评论)

You should implement something like the POST/Redirect/GET pattern and "redirect" to another view at the end of your action methods after all validations were verified and everything executed fine. You can pass entire object instance to the destination view or you just pass plain text message, or you can pull out the text in the destination View itself from web.config or from Resource file.

For instance, I have one view in Shared folder named "ChangeSuccess.aspx" to which I redirect for all my successful edits&creates.

You "redirect" like this

return View("ChangeSuccess", objectInstance);

(note: doesn't actually redirect, see comments)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文