Reputation: 1469
I'm new to using Razor pages, so I have in the model directory a class with the following values:
public int Id { set; get; }
public string CustomerCode { set; get; }
public double Amount { set; get; }
Inside my controller (.cs file), I have the following:
public ActionResult Index()
{
Customer objCustomer = new Customer();
objCustomer.Id = 1001;
objCustomer.CustomerCode = "C001";
objCustomer.Amount = 900.78;
return View();
}
...now, I want to display the values via my Index.cshtml page, but when I run the application, I just get the actual code that I typed as oppose to the values:
...this is how have the .cshtml page setup:
@model Mvccustomer.Models.Customer
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<div>
The customer id is : <%= Model.Id %> <br />
The customer id is : <%= Model.CustomerCode %> <br />
The customer id is : <%= Model.Amount %> <br />
</div>
...my question is, how do I get the values to display? Thanks in advance for any assistance.
Upvotes: 5
Views: 8394
Reputation: 1514
Complete solution to your problem:
Controller Action: You need to send object to view from controller action
public ActionResult Index()
{
Customer objCustomer = new Customer();
objCustomer.Id = 1001;
objCustomer.CustomerCode = "C001";
objCustomer.Amount = 900.78;
return View(objCustomer);
}
View: You need to use @ for Razor syntax
@model Mvccustomer.Models.Customer
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<div>
The customer id is : @Model.Id <br />
The customer id is : @Model.CustomerCode <br />
The customer id is : @Model.Amount <br />
</div>
Upvotes: 0
Reputation: 82267
You need to send the value to the view in the return
return View(objCustomer);
This will allow the model binder to kick in, populating the values of your @model
type with the values from the ActionResult's object.
If you are using razor instead of the <%=
syntax, you should also replace those with the @
razor syntax as shown in Matt Griffiths' answer as well.
Upvotes: 1
Reputation: 1142
You need to use the razor syntax.
Using your example:
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<div>
The customer id is : @Model.Id <br />
The customer id is : @Model.CustomerCode <br />
The customer id is : @Model.Amount <br />
</div>
Upvotes: 4