Knockout和InitializeViewBag方法(MVC) [英] Knockout and InitializeViewBag method (MVC)
本文介绍了Knockout和InitializeViewBag方法(MVC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨!
Visual Studio从下面的示例中不了解方法InitializeViewBag(简单列表)。请帮助
模型
public class SimpleListModel
{
public string ItemToAdd { get; set; }
public List<string> Items { get; set; }
public void AddItem()
{
Items.Add(ItemToAdd);
ItemToAdd = "";
}
}
Razor
Razor
@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);
}
}
推荐答案
(' #myform')。ajaxForm();
< / script >
@ ko.Apply(Model)
('#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);
}
}
这篇关于Knockout和InitializeViewBag方法(MVC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文