Last active
December 23, 2015 08:29
-
-
Save jasonmitchell/6608081 to your computer and use it in GitHub Desktop.
Full sample project: https://github.com/jasonmitchell/aspnetmvc-knockoutjs-quickstart
This file contains 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
using Newtonsoft.Json; | |
using Newtonsoft.Json.Serialization; | |
namespace Quickstart.Web.Extensions | |
{ | |
public static class ObjectExtensions | |
{ | |
public static string ToJson(this object obj) | |
{ | |
JsonSerializerSettings serializerSettings = new JsonSerializerSettings | |
{ | |
ContractResolver = new CamelCasePropertyNamesContractResolver() | |
}; | |
return JsonConvert.SerializeObject(obj, Formatting.None, serializerSettings); | |
} | |
} | |
} |
This file contains 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
using System; | |
namespace Quickstart.Web.Models | |
{ | |
public class Person | |
{ | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
public DateTime DateOfBirth { get; set; } | |
public string EmailAddress { get; set; } | |
public string PhoneNumber { get; set; } | |
} | |
} |
This file contains 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
using System; | |
using System.Web.Mvc; | |
using Quickstart.Web.Models; | |
namespace Quickstart.Web.Controllers | |
{ | |
public class SampleController : Controller | |
{ | |
public ActionResult SimpleModelLoading() | |
{ | |
Person model = new Person | |
{ | |
FirstName = "Jason", | |
LastName = "Mitchell", | |
EmailAddress = "[email protected]", | |
PhoneNumber = "0123456789", | |
DateOfBirth = DateTime.Today.AddYears(-25) | |
}; | |
return View(model); | |
} | |
} | |
} |
This file contains 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
@using Quickstart.Web.Extensions | |
@model Quickstart.Web.Models.Person | |
@section scripts | |
{ | |
<script type="text/javascript" src="/Scripts/ViewModels/SimpleModelLoading.js"></script> | |
<script type="text/javascript"> | |
var viewModel = new SimpleModelLoading(@Html.Raw(Model.ToJson())); | |
ko.applyBindings(viewModel); | |
</script> | |
} | |
<h2>Simple Model Loading Sample</h2> | |
<div> | |
<label>First Name:</label> | |
<input type="text" data-bind="value:firstName"/> | |
</div> | |
<div> | |
<label>Last Name:</label> | |
<input type="text" data-bind="value: lastName"/> | |
</div> | |
<div> | |
<label>Email Address:</label> | |
<input type="text" data-bind="value: emailAddress"/> | |
</div> | |
<div> | |
<label>Phone Number:</label> | |
<input type="text" data-bind="value: phoneNumber"/> | |
</div> | |
<div> | |
<label>Date of Birth:</label> | |
<input type="text" data-bind="value: dateOfBirth"/> | |
</div> | |
This file contains 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
var SimpleModelLoading = function (data) { | |
var self = this; | |
self.firstName = ko.observable(data.firstName); | |
self.lastName = ko.observable(data.lastName); | |
self.emailAddress = ko.observable(data.emailAddress); | |
self.phoneNumber = ko.observable(data.phoneNumber); | |
self.dateOfBirth = ko.observable(data.dateOfBirth); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment