Hi Everyone,
I'm going to share code sample for display student matters using Knockoutjs, mvc 4, json and ajax.
Simple Application using Knockout jQuery, Ajax, Json and MVC 4
Table of Contents
1. In the 1st step, code-sample for view-Model.
2. In the 2nd step, code-sample for View.
3. In the 3rd step, code-sample for Model.
In the 1st step, code sample for view Model
In the 2nd step, code sample for view
In the 3rd step, code sample for Model
I'm going to share code sample for display student matters using Knockoutjs, mvc 4, json and ajax.
Simple Application using Knockout jQuery, Ajax, Json and MVC 4
Table of Contents
1. In the 1st step, code-sample for view-Model.
2. In the 2nd step, code-sample for View.
3. In the 3rd step, code-sample for Model.
In the 1st step, code sample for view Model
<script type="text/javascript">
//
Initialized the matters namespace.
var studentMatters= {};
// View
model declaration.
studentMatters.initViewModel = function (matters) {
var matterViewModel = {
studMatterId:
ko.observable(matters.studMatterId),
studMatterTitle:
ko.observable(matters.studMatterTitle),
studMatterComment: ko.observable(matters.studMatterComment),
createdOn:
ko.observable(matters.createdOn)
};
return matterViewModel;
}
// Bind
to the matters.
studentMatters.bindData = function (matters) {
//
Create the view model
var viewModel =
studentMatters.initViewModel(matters);
//applying
knockout bindings.
ko.applyBindings(viewModel);
}
studentMatters.getmatter = function (studMatterId) {
$.ajax({
url: "/StudentController/Matters/",
type: 'post',
data: "{'studMatterId':'stud01' }",
contentType: 'application/json',
success: function (result) {
studentMatters.bindData(result);
console.log(result);
},
error: function (jqXHR, textStatus,
errorThrown) {
$('#studMessage').html(jqXHR.responseText);
console.log(textStatus);
}
});
}
$(document).ready(function () {
studentMatters.getmatter(stud01);
});
</script>
<div data-role="listview">
<span data-bind="text: studMatterId"></span>
<p data-bind="text: studMatterTitle" />
<p data-bind="text: createdOn" />
<p data-bind="text: studMatterComment" />
</div>
In the 3rd step, code sample for Model
public class StudentController : Controller
{
public ActionResult Index()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult Matters(int MatterId)
{
stdMatter matter = new stdMatter
{
studMatterId = MatterId,
studMatterTitle = "about knockoutjs.",
createdOn = DateTime.Now,
studMatterComment = "Something details for knockoutjs."
};
return Json(matter);
}
public class stdMatter
{
public int studMatterId { get; set; }
public string studMatterTitle { get; set; }
public DateTime createdOn { get; set; }
public string studMatterComment { get; set; }
}
}