Just execute a function that returns a List , and then call it both on page loading and in the AJAX action method.
Something like:
public List<string> GetFolderList() { List<String> folderList = new List<String>(); folderList.Add("East Midlands"); folderList.Add("West Midlands"); folderList.Add("South West"); folderList.Add("North East"); folderList.Add("North West"); return folderList; }
Then, when loading the page, you can stick to this in your model:
public ActionResult Index() { var model = new YourViewModel(); //whatever type the model is model.FolderList = GetFolderList(); //have a List<string> called FolderList on your model return View(model); //send model to your view }
Then, in your opinion, you can:
@model YourViewModel @{ foreach(var item in Model.FolderList){ //do whatever you want } }
Then, if your ajax request was something like this:
$.ajax({ url: '@Url.Action("GetFolders", "ControllerName")', type: 'POST', datatype: 'json', success: function (result) { for (var i = 0; i < result.length; i++) { //do whatever with result[i] } } });
Your GetFolders action GetFolders will look like this:
public ActionResult GetFolders() { return Json(GetFolderList()); }
mattytommo
source share