Mvc闪存消息作为部分视图

4
我正在使用 FlashHelper 向用户显示闪存消息,我需要显示部分视图而不是默认闪存消息。 我该怎么做? 这是否可能?
我需要像这样的辅助程序:
Flash.Success("_FlashMessagePartial",model)
或者你有什么库建议吗?

什么是闪存消息? - TGlatzer
@TGlatzer 我认为这是一条标准的通知消息,它将在几秒钟后自动消失,因此称之为“flash”。优点是您无需在另一个ajax操作之前手动隐藏消息。 - Muflix
1个回答

0

你不能这样使用。如果我理解正确,你想向用户显示类似通知的东西,而且你不想刷新页面。正确的答案是你可以做到,但这并不正确。你的代码看起来会很混乱,你将有一个隐藏字段用于通知等等。这取决于你想要实现什么。例如: 在我的布局中,我使用全局通知,我的代码如下:

在布局顶部,在 我放置了一个if语句,检查TempData中是否存在错误消息

@if(TempData["error"])
{
   // here you can visualize it well styled by some template for example
   <div class="notification-error">@TempData[error]</div>
}
@if(TempData["success"])
{
   // here you can visualize it well styled by some template for example
   <div class="notification-error">@TempData[error]</div>
}

Then in your controller if you have something to say to the user you can
just do what you are doing like:

public ActionResult SomeAction(MyModel model)
{
    if(something is not valid)
    {
       this.TempData["error"] user error
       return this.View(model);
    }
}



That case was if you need some server validation and proper message to the     
user. After that option you have to set a script on your layout that removes
the error and success notifications if they exist on the page loads. 

如果您需要在客户端进行一些模型状态验证,您可以查看由视图模型属性驱动的js-unobtrusive验证,它会在将数据发送到服务器之前自动在客户端进行检查。

第三个选项是,如果您需要其他通知,可以使用一个名为toastr的JS库。 示例 库网址 它很轻便且非常易于使用。

希望这个答案正好解释了您所需的内容。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接