简体   繁体   English

从 controller 发送消息以在 asp.net 核心 mvc 中查看的最佳方式

[英]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.

相关问题 ASP.Net Core-如何在不刷新的情况下从控制器发送消息以进行查看? - ASP.Net Core - How to send message to view from controller without refreshing? MVC ASP.NET从View到Controller获取52个文本框键/值的最佳方法 - MVC ASP.NET Best way to get 52 textbox key/values from View to Controller ASP.NET MVC - 从视图中创建 Url 到 controller 动作的最佳方法是什么? - ASP.NET MVC - What's the best way to create a Url to controller action from within view? 从控制器到视图的ASP.NET MVC消息框 - ASP.NET MVC message box from controller to view 在控制器 asp.net mvc 5 的视图上显示错误消息 - Display error message on the view from controller asp.net mvc 5 将ViewBag中的消息从控制器传递到ASP.Net MVC中的视图 - Passing message in ViewBag from controller to view in ASP.Net MVC c# asp.net core mvc 将数据从视图传递到控制器并从控制器返回到视图 - c# asp.net core mvc passing data from view to controller and from controller back to view Asp.net 核心 mvc 将 int 数组发送到 controller - Asp.net core mvc send array of int to controller 将两个或多个参数从 model 视图传递到 EF Core / ASP.NET Core MVC 应用程序中的 controller - Passing two or more parameters from model view to controller in EF Core / ASP.NET Core MVC application 如何从 ASP.NET Core MVC 中的一个控制器调用或返回另一个控制器的视图 - How to call or return the view of another controller from one controller in ASP.NET Core MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM