Tharushi Geethma
Tharushi Geethma

Reputation: 1289

How to display a calculated value in a view?

I'm new to ASP.NET and MVC architecture. I want to display a number in a view after doing a calculation as follows

the total is here[![]

How can I display the totalmessage in a view?

Upvotes: 0

Views: 2955

Answers (2)

Ivan Mladenov
Ivan Mladenov

Reputation: 1847

You can use a view model to hold your data.

public class TotalViewModel 
{
  public int Total {get; set;}

  public string Message {get; set;}
}

Then in the controller

public ActionResult Calculate()
{
  // do your calculations...

  var model = new TotalViewModel { Message = "Total = ", Total = total };

  return this.View(model);
}

And the view

@model Path.To.Your.TotalViewModel

<p>@Model.Message @Model.Total</p>

Upvotes: 3

user7557524
user7557524

Reputation:

You can use ViewBag for any data to pass view, add these code to the Calculate method you have;

ViewBag.total = totalMessage;

than in the Calculate.cshtml view;

<p>@ViewBag.total</p>

that's all

Upvotes: 0

Related Questions