4

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"] %> 

Instead I need the value of currenttime.

Vadim Martynov
  • 8,602
  • 5
  • 31
  • 43
Vishwa Raj
  • 57
  • 1
  • 9

3 Answers3

4

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

<div>
    @DateTime.Now
</div>

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

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

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

will be rendered to

<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:

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. 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; }
    }
    
  2. Pass model object to the view:

    public ActionResult Index()
    {
        var model = GetModel();
        return View(model);
    }
    
  3. Add strongly typed View via define model type:

    @model Person
    
  4. Use Model variable in your view:

    @Model.City
    
Community
  • 1
  • 1
Vadim Martynov
  • 8,602
  • 5
  • 31
  • 43
  • It is not the matter of only DateTime... Similarly, I wanted to pass the variable. Like ViewData["myname"] = "Vishwa raj"; – Vishwa Raj Jun 14 '16 at 18:05
  • 1
    @VishwaRaj I updated my answer with simple example of MVC pattern and links to Microsoft tutorials for asp.net mvc. – Vadim Martynov Jun 14 '16 at 20:21
1

you can pass values (also complex objects) as model i.e. a DateTime Object

public ActionResult Upload()
{

    ViewBag.Message = "Make a quiz Question here";
    return View(DateTime.Now);

}

and in the first line your view you have to define which datatype the model has. Than you can access to it through Model.something:

@model System.DateTime

<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>
<div>
    @Model.ToString()
</div>

Here you have an executable example: https://dotnetfiddle.net/mIgpJL

Maximilian Ast
  • 3,369
  • 12
  • 36
  • 47
1

You're mixing Razor and ASPX syntax. Change your view to:

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

I would suggest reading up on ViewBag and ViewData, and on Razor model binding.

Spivonious
  • 718
  • 7
  • 20