I have a viewmodel with the following property:
public class CompanyDetailsViewModel
{
[Required(ErrorMessage = "A Company Name is required")]
[Display(Name = "Company Name:")]
[StringLength(100)]
public string CompanyName { get; set; }
...
public IList<SuburbAndPostcode> SuburbAndPostcodesList { get; set; }
}
The list was created from this POCO class:
public class SuburbAndPostcode
{
[Key]
public int SuburbsAndPostcodesId { get; set; }
public string Suburb { get; set; }
public string PostCode { get; set; }
public State State { get; set; }
}
This is the state object:
public class State
{
[Key]
public int StateId { get; set; }
public string ShortName { get; set; }
public string Name { get; set; }
public virtual ICollection<CompanyDetail> CompanyDetails { get; set; }
}
I am trying to create a variable with the suburb and postcode properties as a list that I can use for an autocomplete function however I cant seem to assign the Model.SuburbsAndPostCodesList to a variable.
I have tried various javascript options indicated from other questions on this forum like here.
I would like to have a javascript variable that represents the list and I have tried just setting:
var suburbs = @Model.SuburbAndPostcodesList
I've tried using json and I have tried looping through the Model but get an error saying that the variable "test" is out of context:
var test =[];
@for (int i = 0; i < Model.SuburbAndPostcodesList.Count; i++)
{
test[i]=...
}
I have also tried using "push" and ".Encode".
I would like to know how to assign this list of strings and state object to a javascript variable?