【问题标题】:Knockout and InitializeViewBag method (MVC)淘汰赛和 InitializeViewBag 方法 (MVC)
【发布时间】:2013-12-17 03:08:12
【问题描述】:

Visual Studio 不理解下面示例中的方法 InitializeViewBag("Simple list")。 它是如何制作的?有这种方法吗?

为什么在这个例子中,使用它:Example

型号

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

控制器

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

【问题讨论】:

  • 你的意思是“Visual Studio 不理解方法 InitializeViewBag("Simple list")”?你有编译错误吗?也许您没有添加对所需库的引用?
  • 错误:InitializeViewBag 不存在

标签: c# asp.net-mvc asp.net-mvc-4 razor knockout.js


【解决方案1】:

查看 github 中 BaseController 的 source code,我可以看到 InitializeViewBag 方法在那里定义。这就是 SimpleListController 可以访问它的方式。如果你有 BaseController 的完全相同的实现并且仍然无法识别该方法,我会认为这很烦人,比如这里或那里有一个额外的括号,或者在其他地方有一个重复的 BaseController。

【讨论】:

    猜你喜欢
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 2015-03-01
    • 2015-03-12
    • 2012-11-25
    • 2013-11-01
    • 2014-04-13
    • 1970-01-01
    相关资源
    最近更新 更多