1 回答
TA贡献1858条经验 获得超8个赞
有趣的问题:)
最初的解决方案是实现一个自定义ActionResult,该自定义将覆盖ExecuteResult设置TempData消息的方法。
创建自定义操作结果: `
public class RedirectWithConfirmationMessageResult : ActionResult
{
private readonly string message;
public RedirectWithConfirmationMessageResult(ActionResult redirectBaseResult, string message)
{
BaseResult = redirectBaseResult;
this.message = message;
}
public ActionResult BaseResult { get; private set; }
public override void ExecuteResult(ControllerContext context)
{
context.Controller.TempData["Message.Notification"] = this.message;
BaseResult.ExecuteResult(context);
}
}
创建扩展方法
public static class RedirectExtensions
{
public static RedirectWithConfirmationMessageResult WithConfirmation(this RedirectToRouteResult instance, string message)
{
return new RedirectWithConfirmationMessageResult(instance, message);
}
public static RedirectWithConfirmationMessageResult WithConfirmation(this RedirectResult instance, string message)
{
return new RedirectWithConfirmationMessageResult(instance, message);
}
}
用法
[HttpGet]
public ActionResult DoSomething()
{
return this
.RedirectToAction("Index", "Home")
.WithConfirmation("Saved Successfully !");
}
希望有所帮助。
- 1 回答
- 0 关注
- 149 浏览
添加回答
举报