成功消息与状态错误消息模型相反

时间:2022-08-29 22:45:17

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.

但是,你如何看待成功的回应,比如“你成功地把很多钱转给了你的前任”。+“你的余额现在是零”。我仍然希望将它设置在控制器级别,最好是使用键-值方式,与errormessage相同,但不会使modelstate无效。

How is this usually done? ViewData?

这通常是怎么做到的?显示数据?

3 个解决方案

#1


22  

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[“成功”](或您想要给它的关键字)和我想要在控制器中显示的消息,然后适当地重定向(例如,如果我编辑一个用户,我将重定向回用户列表)。这依赖于POST/Redirect/GET模式——这是一个很好的实践。

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):

在主页中,我有一个部分检查该变量并以一个漂亮的div显示消息。

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

#2


7  

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

我想你可以检查modelstate并在你的模型中设置一个变量……

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.

编辑:考虑到你最新的问题,我认为你在寻找错误的方法。我会跟着其他答案走,遵循PRG模式。这肯定比添加假错误更有意义。

#3


0  

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.

您应该实现一些东西,比如POST/Redirect/GET模式和“重定向”到操作方法末尾的另一个视图,在所有验证和所有执行良好之后。您可以将整个对象实例传递给目标视图,或者只传递纯文本消息,或者从web中提取目标视图本身中的文本。配置或从资源文件。

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

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

You "redirect" like this

你这样的“定向”

return View("ChangeSuccess", objectInstance);

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

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

#1


22  

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[“成功”](或您想要给它的关键字)和我想要在控制器中显示的消息,然后适当地重定向(例如,如果我编辑一个用户,我将重定向回用户列表)。这依赖于POST/Redirect/GET模式——这是一个很好的实践。

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):

在主页中,我有一个部分检查该变量并以一个漂亮的div显示消息。

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

#2


7  

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

我想你可以检查modelstate并在你的模型中设置一个变量……

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.

编辑:考虑到你最新的问题,我认为你在寻找错误的方法。我会跟着其他答案走,遵循PRG模式。这肯定比添加假错误更有意义。

#3


0  

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.

您应该实现一些东西,比如POST/Redirect/GET模式和“重定向”到操作方法末尾的另一个视图,在所有验证和所有执行良好之后。您可以将整个对象实例传递给目标视图,或者只传递纯文本消息,或者从web中提取目标视图本身中的文本。配置或从资源文件。

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

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

You "redirect" like this

你这样的“定向”

return View("ChangeSuccess", objectInstance);

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

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