如何从控制器传递数据以在ASP.net MVC中查看? [英] How to pass data from controllers to view in ASP.net MVC?

查看:91
本文介绍了如何从控制器传递数据以在ASP.net MVC中查看?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

net MVC.

对于我认识的每个人,我需要做的非常简单. 我需要在控制器之间传递数据才能在ASP.net MVC中查看.

What I need to do is very simple for everyone I know. I need to pass data between controllers to view in ASP.net MVC.

我在控制器中编写的代码.

Code I have written in controllers.

public ActionResult Upload()
{

    ViewBag.Message = "Make a quiz Question here";
    ViewData["CurrentTime"] = DateTime.Now.ToString();
    return View();
}

这是我在视图中添加的代码.

and this is the code I have added in views.

<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>
<div>
    <%: ViewData["CurrentTime"] %>
</div>

但是当我运行该程序时,它会显示

But when I run this program it shows

<%: ViewData["CurrentTime"] %> 

相反,我需要currenttime的值.

Instead I need the value of currenttime.

推荐答案

您无需从控制器传递诸如当前时间之类的数据.你可以写

You need not to pass data like current time from the controller. You can just write

<div>
    @DateTime.Now
</div>

在您看来.在服务器中渲染的视图将被渲染为@DateTime.Now的值.

in your view. View rendered in the server then it will be render to the value of @DateTime.Now.

此外,您可以使用 ToString 格式化视图的方法:

Also, you can use ToString method to format your view:

<div>
    @DateTime.Now.ToString("yyyy.MM.dd")
</div>

将呈现给

<div>
    2016.06.14
</div>

请注意,代码将显示服务器时间,该时间可能与客户端的时区不同.要显示当前时间,您应该

Note that code will display server time that can be different to the client's timezone. To display current time you should use JS code.

这不是静态数据,但实际上也不是模型或业务数据,可以在没有和输入参数的情况下在视图中进行计算.消息也不是模型,也不是控制器相关的数据,它可以在视图中进行硬编码或从模型数据中检索:

It's not static data but it's also not really Model or business data and it can be calculated in the view without and input parameters. Message is also not a model and not controller-dependent data and it can be hardcoded in the view or retrieved from the model data:

<h3>Make a quiz Question here</h3>

如果您实际上需要从控制器传递数据,并且其数据取决于内部状态或输入控制器参数或具有业务数据"的其他属性,则应使用

If you need to pass data actually from the controller and its data is depend on internal state or input controller parameters or has other properties of "business data" you should use Model part from MVC pattern:

模型对象是应用程序中实现以下目的的部分 应用程序数据域的逻辑.通常,模型对象检索 并将模型状态存储在数据库中.例如,一个Product对象 可能会从数据库中检索信息,对其进行操作,然后 将更新的信息写回到SQL Server中的产品表 数据库.

Model objects are the parts of the application that implement the logic for the application's data domain. Often, model objects retrieve and store model state in a database. For example, a Product object might retrieve information from a database, operate on it, and then write updated information back to a Products table in a SQL Server database.

您可以在此处查看详细信息,或查看

You can see details here or look to the Models and Validation in ASP.NET MVC part of Microsoft tutorial.

  1. 添加模型类:

  1. Add model class:

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public int Zipcode { get; set; }
}

  • 将模型对象传递给视图:

  • Pass model object to the view:

    public ActionResult Index()
    {
        var model = GetModel();
        return View(model);
    }
    

  • 添加强类型的视图通过定义模型类型:

  • Add strongly typed View via define model type:

    @model Person
    

  • 在视图中使用Model变量:

    @Model.City
    

  • 这篇关于如何从控制器传递数据以在ASP.net MVC中查看?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆