Knockout MVC4 KnockoutJs using JSON and Ajax

Knockout MVC4 KnockoutJs using JSON and Ajax

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



<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>


In the 2nd step, code sample for view


 <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; }
    }

}
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

Knockout MVC4 KnockoutJs using JSON and Ajax Knockout MVC4 KnockoutJs using JSON and Ajax Reviewed by Unknown on 7:12 AM Rating: (5)
www.code-sample.com/. Powered by Blogger.
^