[英]Best way to send message from controller to view in asp.net core mvc
I'm using asp.net core mvc (latest version).我正在使用 asp.net 核心 mvc(最新版本)。
I always send my messages (success or failed messages) from controller to view by TempData like this:我总是从 controller 发送我的消息(成功或失败消息),以便通过 TempData 查看,如下所示:
TempData["msg"] = "Operation was successful";
and in the view show this message to user:并在视图中向用户显示此消息:
@if (TempData["msg"] != null)
{
string msg = (string)TempData["msg"];
<script>
UIkit.notification({ message: '@msg', status: 'primary', pos: 'bottom-center', timeout: 15000 });
</script>
}
i want to know the standard (best and optimized) way to pass this kind of messages from controller to view.我想知道将此类消息从 controller 传递给查看的标准(最佳和优化)方式。
is TempData good for this case? TempData 适合这种情况吗? is there any better way?
有没有更好的方法?
Thanks.谢谢。
You usually send a "Model" to your "View" through "Controller" that's what MVC stands for of course:您通常通过“控制器”将“模型”发送到您的“视图”,这当然是 MVC 所代表的:
public class SendMessageModel
{
public int Id { get; set; }
public string Name { get; set; }
//... More stuff
}
//... inside controller
var model = new SendMessageModel()
{
Id = 1,
Name = "Name"
};
return View(model);
//... inside view
@model SendMessageModel
<span>@Model.Id @Model.Name</span>
but sometimes you need to send additional info to the view.但有时您需要向视图发送附加信息。 This data is not necessarily part of the model.
此数据不一定是 model 的一部分。 For this kind of scenario using ViewData is my choice.
对于这种使用 ViewData 的场景是我的选择。
public async Task<ActionResult> Index(bool? error)
{
if (error == true)
{
ViewData["Message"] = "Some error message";
}
//....
return View();
}
//... inside view
@model SendMessageModel
@{
var msg = ViewData["Message"];
}
@if(msg != null)
{
<span class="error">@msg</span>
}
of course, this is not the only way or "the best way".当然,这不是唯一的方式或“最好的方式”。 There's always another way to do this and that's the beauty of Asp.Net Core or programming in general.
总有另一种方法可以做到这一点,这就是 Asp.Net Core 或一般编程的美妙之处。
Create a model to represent all data you are going to display in the view.创建一个 model 来表示您将在视图中显示的所有数据。
public class NewInvoiceViewData
{
public Invoice Invoice { get; set; }
public string Message { get; set; }
public bool HasMessage => string.IsNullOrEmpty(Message) == false;
}
public async Task<ActionResult> CreateInvoice(NewInvoiceParameters parameters)
{
var invoice = createInvoice.From(parameters);
var message = invoice == null ? "Something gone wrong" : "Success";
var viewData = new NewInvoiceViewData
{
Invoice = invoice,
Message = message
}
return View(viewData);
}
In view在视图中
@if (@data.HasMessage)
{
<script>
UIkit.notification({ message: '@data.Message', status: 'primary', pos: 'bottom-center', timeout: 15000 });
</script>
}
MVC(Model View Controller) is not about application layering. MVC(模型视图控制器)与应用程序分层无关。 Model isn't required to be a business objects or database tables.
Model 不需要是业务对象或数据库表。 Notice, that it can be a business object or database datable if no extra values are required, but it is not mandatory.
请注意,如果不需要额外的值,它可以是业务 object 或数据库数据,但这不是强制性的。
Model is an object which view know how to display without extra workarounds and model can have all data required and possible prepared for the view. Model 是一个 object 视图知道如何在没有额外解决方法的情况下显示,model 可以为视图准备所有需要和可能的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.