成功消息而不是模型状态错误消息
对于错误消息、验证错误等,您有
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将使用我想要在控制器中显示的消息填充 TempData["success"] (或您想要给它的任何键),然后适当地重定向(例如,如果我编辑用户,我会重定向回用户列表) 。这依赖于 POST/Redirect/GET 模式——无论如何,这是一个很好的实践。
在母版页中,我有一个部分检查该变量并在样式漂亮的 div 中显示消息。像这样的东西(可能不是100%正确):
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.
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):
我想您可以检查模型状态并在模型中设置一个变量...
在您看来...
编辑:鉴于您更新的问题,我认为您正在考虑采取错误的方法。我会同意其他答案并遵循 PRG 模式。这绝对比尝试添加假错误更有意义。
I suppose you could check the modelstate and set a variable in your model...
In your view...
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.
您应该实现类似 POST/Redirect/GET 模式的内容,并在验证所有验证并且一切正常执行后,在操作方法末尾“重定向”到另一个视图。您可以将整个对象实例传递到目标视图,也可以仅传递纯文本消息,也可以从 web.config 或资源文件中提取目标视图本身中的文本。
例如,我在名为“ChangeSuccess.aspx”的共享文件夹中有一个视图,我将所有成功的编辑和创建重定向到该视图。
你像这样“重定向”
(注意:实际上并没有重定向,请参阅评论)
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
(note: doesn't actually redirect, see comments)