Skip to content

Instantly share code, notes, and snippets.

@bsatrom
Created July 27, 2011 18:37
Show Gist options
  • Save bsatrom/1110056 to your computer and use it in GitHub Desktop.
Save bsatrom/1110056 to your computer and use it in GitHub Desktop.
KnockoutJS and ASP.NET MVC strongly-typed helpers
<fieldset>
<legend>Speaker Info</legend>
Name: <input type="text" data-bind="value: name" /> <br />
Bio: <textarea data-bind="value: bio"></textarea> <br />
Twitter Handle: <input type="text" data-bind="value: twitterHandle" /> <br />
State: <input type="text" data-bind="value: state" /> <br />
</fieldset>
<fieldset>
<legend>Speaker Info</legend>
Name: @Html.TextBoxFor(m => m.Name, new { data_bind= "value: name" }) <br />
Bio: @Html.TextAreaFor(m => m.Bio, new { data_bind= "value:bio" }) <br />
Twitter Handle: @Html.TextBoxFor(m => m.TwitterHandle, new { data_bind= "value: twitterHandle" }) <br />
State: @Html.TextBoxFor(m => m.State, new { data_bind= "value: state" }) <br />
</fieldset>
modelBinder.createBindings = function (bindlist) {
function setBinding(id, value) {
var el = getElement(id);
if (el) {
el.setAttribute('data-bind', value);
}
}
function getElement(id) {
var el = document.getElementById(id);
if (!el) {
id = id.charAt(0).toUpperCase() + id.slice(1);
el = document.getElementById(id);
}
return el;
}
/* other code here */
};
<fieldset>
<legend>Speaker Info</legend>
Name: @Html.TextBoxFor(m => m.Name) <br />
Bio: @Html.TextAreaFor(m => m.Bio) <br />
Twitter Handle: @Html.TextBoxFor(m => m.TwitterHandle) <br />
State: @Html.TextBoxFor(m => m.State) <br />
</fieldset>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment