Last active
December 11, 2017 18:06
-
-
Save ChrisMoney/70c25f03f2876c2ea7749c697672aa2d to your computer and use it in GitHub Desktop.
Bind Drop Down in Knockout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<section> | |
<label class="label">To: </label> | |
<label class="select"> | |
<select class="form-control input-sm" data-bind="options: emailList, | |
optionsText: 'Name', | |
optionsValue: 'ID', | |
optionsCaption: '-- Select --'"></select> | |
<i></i> | |
</label> | |
</section> | |
// Controller | |
[HttpPost] | |
public async Task<JsonResult> GetTaskSourceEmails(int taskSourceId) | |
{ | |
using (var tsEmailMgr = new TaskSourceEmailMgr()) | |
{ | |
var taskSourceEmailList = (await tsEmailMgr.GetTaskSourceEmails(taskSourceId)).Select(x => new LookUp | |
{ | |
ID = x.TaskSourceEmailID, | |
Name = x.TaskSourceEmail | |
}); | |
return Json(new { taskSourceEmailList, JsonRequestBehavior.AllowGet}); | |
} | |
} | |
// Business Logic | |
public async Thread.Task<List<TaskSourceEmailDTO>> GetTaskSourceEmails(int taskSourceId) | |
{ | |
var query = (from ts in m_context.TaskSourceEmails | |
where ts.TaskSourceID == taskSourceId | |
select new TaskSourceEmailDTO | |
{ | |
TaskSourceID = ts.TaskSourceID, | |
TaskSourceEmailID = ts.TaskSourceEmailID, | |
TaskSourceEmail = ts.TaskSourceEmail | |
} | |
); | |
return await query.Distinct().ToListAsync(); | |
} | |
// Knockout - parameters in knockout must match parameters coming from Controller | |
// get values in mapping function | |
var mapData = function (data) { | |
self.sourceId(data.detail.Task.TaskSourceId); | |
var taskSourceId = data.detail.Task.TaskSourceId; | |
$.post("/Task/GetTaskSourceEmails/", { taskSourceId: taskSourceId }, function (data) { | |
self.emailList(data.taskSourceEmailList); | |
}); | |
} | |
// mapping is called in Load() function | |
function Load(allData) { | |
// Load initial state from server | |
if ($.isEmptyObject(allData)) { | |
$.post("/Task/GetDetail", { taskId: detailId }, function success(data, textStatus, jqXHR) { | |
mapData(data); | |
}); | |
} else { | |
mapData(allData); | |
} | |
//Builds task history and set the timer to auto refresh | |
getTaskHistory(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment