0

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

3
  • It is not clear what does not work when you do var listModel = document.getElementById("mainOfferDiv"); you perhaps want to add .childNodes or something? Commented Nov 3, 2021 at 7:52
  • 1
    I think you should learn the basics first, otherwise things will get really confusing. CSHTML only exists server-side. Commented Nov 3, 2021 at 7:52
  • mplungjan Sorry for question is not that proper, and Asif give me proper output thank you Commented Nov 3, 2021 at 8:17

1 Answer 1

1

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

1 Comment

Thank you Asif var model = @Html.Raw(Json.Encode(Model)); worked for me

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.