Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
how to get model values in javascript

testModel

public class testModel
{
  public int ID{get; set;}
  public string name{get; set;}
}

cshtml code

@model IEnumerable<testModel>
var lstModel = Model.ToList();
<div id="mainOfferDiv">
@for(int i = 0; i<lstModel.Count(); i++)
{
}
</div

In js file i want to get list of testModel data which is there in cshtml file, I tried with below

js file

var listModel = document.getElementById("mainOfferDiv");

Suppose lstModel contains list of 10data i want to get same in js file

Thank you in advance

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could take your entire server-side model and turn it into a Javascript object by doing the following:

var model = @Html.Raw(Json.Encode(Model));

In your case if you just want the FloorPlanSettings object, simply pass the Encode method that property:

var floorplanSettings = @Html.Raw(Json.Encode(Model.FloorPlanSettings));

another way is

Classs property ---
        Name = "Raj",
        IsAuthenticated = true,
        LoginDateTime = DateTime.Now,
        Age = 26,
        UserIconHTML = "<i class='fa fa-users'></i>" 
---------------------------------

js in cshtml

var Name = @Model.Name;  
var Age = @Model.Age;
var LoginTime = @Model.LoginDateTime; 
var IsAuthenticated = @Model.IsAuthenticated;   
var IconHtml = @Model.UserIconHTML; 
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!