如何使用同一控制器操作结果重定向到不同的页面
本文关键字:重定向 结果 何使用 控制器 操作 | 更新日期: 2023-09-27 18:35:31
我有两个使用相同的ActionResult的视图。最初只有一个视图,但现在需要第二个视图。
观点是 - "指数"和"接受"指数
[HttpGet]
public ActionResult Index(string status, string message)
{
var InboxStatus = InboxStatus.New;
if (!Enum.TryParse(status, out inboxStatus))
inboxStatus = InboxStatus.New;
var model = new InboxModel();
model.Status = inboxStatus.ToString();
model.InboxMailCount = GetInboxMailCount();
model.InboxMailCount.Status = InboxStatus.ToString();
@ViewBag.Message = message;
return View(model);
}
接受
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Accept(InboxModel model)
{
if (ModelState.IsValid)
{
var inboxmail = _repo.GetById(model.ID);
inboxmail.Status = (int)ReferralStatus.Accepted;
inboxmail.AcceptedByUserId = UserId;
inboxmail.AcceptenceDateTime = DateTime.Now;
_uow.SaveChanges();
return RedirectToAction("Index", new { Message = "Accepted Successfully" });
}
return View(model.ID.ToString());
}
操作结果称为"已拒绝",问题是操作结果包含以下内容...
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Decline(InboxModel model)
{
if (ModelState.IsValid)
{
var InBox = _repo.GetById(model.ID);
InBox.Status = (int)ReferralStatus.Declined;
InBox.DeclinedByUserId = UserId;
InBox.DeclinedDateTime = DateTime.Now;
_uow.SaveChanges();
return RedirectToAction("Index", new { Message = "Declined Successfully" });
}
return Accept(model.ID.ToString());
}
因此,如果用户在任一页面上选择"已拒绝"操作,则无论他们在"已接受"视图上执行操作,他们都将被重定向到"索引"。有意义?我想将它们重定向回它们来自的页面。
请注意,这就是我目前重定向到"已接受"以进行不同操作的方式......
由于索引中的列表取决于"状态"属性...索引视图..
ASP.Net MVC 4 使用剃刀 2 视图.....有什么想法吗?
您可以使用类似 ViewBag.ReturnUrl
的内容在操作序列中存储最新的 ActionName。
例:
public ActionResult ActionA(){
ViewBag.ReturnUrl = "ActionA";
return View();
}
public ActionResult ActionB(){
ViewBag.ReturnUrl = "ActionB";
return View();
}
public ActionResult Declined(){
return RedirectToAction(ViewBag.ReturnUrl);
}
您可以尝试缓存或保存以前的网址,然后重定向回该网址。
public ActionResult Index()
{
var previousPage = System.Web.HttpContext.Current.Request.UrlReferrer;
//Yourlogic
RedirectToAction(previousPage);
}
只需在收件箱模型中添加一个属性即可说明您的来源。然后,您可以在表单中放置具有该值的隐藏。
顺便说一句,您正在重定向到索引而不传递状态参数...我错了吗?
知道了!!抱歉,这不是一个简单的问题,因为有很多代码会增加混乱,谢谢大家的时间。
基本上在操作开始时保存邮件的初始状态。因为它的状态仅在操作中更改。在"接受的操作结果"的顶部,
var returnStatus = Mail.Status;
然后在返回中使用该变量。
return RedirectToAction("Index", new { Status = (int)returnStatus, Message = "Mail Successfully added to Sent Listing" });