5

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.

3 Answers 3

4

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> 
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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> 

1 Comment

not sure why my answer down voted? Pls update with reason who down voted my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.