public class SimpleListModel { public string ItemToAdd { get; set; } public List<string> Items { get; set; } public void AddItem() { Items.Add(ItemToAdd); ItemToAdd = ""; } }
@using PerpetuumSoft.Knockout @model KnockoutMvcDemo.Models.SimpleListModel @{ var ko = Html.CreateKnockoutContext(); } @using (ko.Html.Form("AddItem", "SimpleList", null, new { id = "myform" })) { <span>New item:</span> @ko.Html.TextBox(m => m.ItemToAdd).ValueUpdate(KnockoutValueUpdateKind.AfterKeyDow n) <button type="submit" @ko.Bind.Enable(m => m.ItemToAdd.Length > 0)>Add</button> <p>Your items:</p> @ko.Html.ListBox(m => m.Items, new { width = 50, size = 7 }) } <script type="text/javascript"> $('#myform').ajaxForm(); </script> @ko.Apply(Model)
public class SimpleListController : BaseController { public ActionResult Index() { InitializeViewBag("Simple list"); var model = new SimpleListModel { Items = new List<string> { "Alpha", "Beta", "Gamma" } }; return View(model); } public ActionResult AddItem(SimpleListModel model) { model.AddItem(); return Json(model); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)