I am passing an object(model) in View where I have javascript code written. The object has certain properties that I want to access in javascript in order to create a drop down list from the values of those properties. Here is my object:
public class TestObject
{
public BuildData ExteriorColor { get; set; }
public BuildData InteriorColor { get; set; }
}
and
public class BuildData
{
public List<ExteriorInteriorData> Data { get; set; }
public bool isInstalled { get; set; }
public BuildData()
{
Data = new List<ExteriorInteriorData>();
}
}
Now in the View I have an object of TestObject through ViewData and I want to populate the values present in List<ExteriorInteriorData> in a select list.
Basically I want to do something like this:
for (var i = 0; i < data.ExteriorColor.Data.length; i++) {
$("#Exterior_Color").append($("<option " + (i == 0 ? "selected" : "") + "></option>").val(data.ExteriorColor.Data[i].ColorName + ", " + data.ExteriorColor.Data[i].RgbValue).html(data.ExteriorColor.Data[i].ColorName));
}
So, How do I access the object TestObject present in Viewdata inside of Javascript?
@Html.DropDownListFor()method). But to answer your question -var model = @Html.Raw(Json.Encode(Model))converts you model to a javascript object.