发回信息给用户[复制]

问题描述 投票:0回答:1

这个问题在这里已有答案:

当我在数据库中发送一些数据并且文本成功时,应该通知它,如果有错误,它应该给出错误。

消息必须显示在“创建”或“登录”上,但这取决于它是成功还是错误。

AccountViews model = new AccountViews();
var mail = model.Email;
var result = _dbContext.Users.FirstOrDefault(i => i.Brugernavn == mail);
if(result == null)
{

    model.Message = "Succes";

    return RedirectToAction("Login");
}

model.Message = "Error";

return RedirectToAction("Create");

在我的模型中,我有:

[TempData]
public string Message { get; set; }
public bool ShowMessage => !string.IsNullOrEmpty(Message);

那么这里的问题是如何让我的信息出现?

问题是,正如我所看到的,当我将我/某人发送到另一个页面时,我如何将我的消息显示在页面上。

我在MVC工作 - .Net core 2.0

c# asp.net-core-mvc
1个回答
0
投票

你可以使用这样的查询字符串传递它:

RedirectToAction("Create", new { error= "ErrorMsg" });

因此,要在目标操作中获取此操作,您应该为create操作添加一个参数:

public ActionResult Create(string error){
//do what you want with error
//to show this in view you can use viewbag like:
ViewBag.Error = error;
return View();
}

然后你可以像在Create视图文件中调用它一样简单地显示它:

<div> @ViewBag.Error <div>
© www.soinside.com 2019 - 2024. All rights reserved.