【问题标题】:Can you use ViewBag as a datasource in an MVC 3 WebGrid?您可以使用 ViewBag 作为 MVC 3 WebGrid 中的数据源吗?
【发布时间】:2011-03-13 22:42:15
【问题描述】:

如果是这样,语法是什么。我没有找到太多文档,而且我所拥有的也不起作用。

控制器类:

public ActionResult Results()
    {                   

        List<GMUOverview> _results = new List<GMUOverview>
        {
            new GMUOverview { GMU = "EE00101R", UnitsIncluded = "12,13,14", SuccessRate = "19%", MinReqResPoints = "3", MinReqNResPoints="5", AvailableLicenses="123", TotalApplications=221, Season="1", Year="2009" },
            new GMUOverview { GMU = "EE00102R", UnitsIncluded = "12,13,64", SuccessRate = "19%", MinReqResPoints = "3", MinReqNResPoints="5", AvailableLicenses="123", TotalApplications=221, Season="1", Year="2009" },
            new GMUOverview { GMU = "EE00103R", UnitsIncluded = "12,43,14", SuccessRate = "21%", MinReqResPoints = "4", MinReqNResPoints="5", AvailableLicenses="123", TotalApplications=221, Season="1", Year="2009" },
            new GMUOverview { GMU = "EE00104R", UnitsIncluded = "22,13,14", SuccessRate = "22%", MinReqResPoints = "5", MinReqNResPoints="5", AvailableLicenses="123", TotalApplications=221, Season="1", Year="2009" },
            new GMUOverview { GMU = "EE00105R", UnitsIncluded = "12,13,14", SuccessRate = "33%", MinReqResPoints = "6", MinReqNResPoints="5", AvailableLicenses="123", TotalApplications=221, Season="1", Year="2009" },
            new GMUOverview { GMU = "EE00106R", UnitsIncluded = "12,13,14", SuccessRate = "44%", MinReqResPoints = "7", MinReqNResPoints="5", AvailableLicenses="123", TotalApplications=221, Season="1", Year="2009" },
        };


        ViewBag.Result = _results;        

        return View();
    }

查看:

@using (Html.BeginForm())
{   

@{
    var grid = new WebGrid(ViewBag.Result);

      <div id="grid"> 
        @grid.GetHtml() 
    </div> 

 }

}

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-3 razor


    【解决方案1】:

    您为什么使用ViewBag 而不是视图模型和强类型视图?这是丑陋/糟糕/弱输入/废话/(在这里放任何你能想到的坏词/诅咒)?

    如果尽管我咆哮你还是决定使用ViewBag,那么正确的语法如下:

    @{
        var grid = new WebGrid(ViewBag.Result);
    }
    
    @using (Html.BeginForm())
    {   
        <div id="grid"> 
            @grid.GetHtml() 
        </div> 
    }
    

    当然是使用视图模型的正确方法:

    public ActionResult Results()
    {                   
        var _results = new List<GMUOverview>
        {
            new GMUOverview { GMU = "EE00101R", UnitsIncluded = "12,13,14", SuccessRate = "19%", MinReqResPoints = "3", MinReqNResPoints="5", AvailableLicenses="123", TotalApplications=221, Season="1", Year="2009" },
            new GMUOverview { GMU = "EE00102R", UnitsIncluded = "12,13,64", SuccessRate = "19%", MinReqResPoints = "3", MinReqNResPoints="5", AvailableLicenses="123", TotalApplications=221, Season="1", Year="2009" },
            new GMUOverview { GMU = "EE00103R", UnitsIncluded = "12,43,14", SuccessRate = "21%", MinReqResPoints = "4", MinReqNResPoints="5", AvailableLicenses="123", TotalApplications=221, Season="1", Year="2009" },
            new GMUOverview { GMU = "EE00104R", UnitsIncluded = "22,13,14", SuccessRate = "22%", MinReqResPoints = "5", MinReqNResPoints="5", AvailableLicenses="123", TotalApplications=221, Season="1", Year="2009" },
            new GMUOverview { GMU = "EE00105R", UnitsIncluded = "12,13,14", SuccessRate = "33%", MinReqResPoints = "6", MinReqNResPoints="5", AvailableLicenses="123", TotalApplications=221, Season="1", Year="2009" },
            new GMUOverview { GMU = "EE00106R", UnitsIncluded = "12,13,14", SuccessRate = "44%", MinReqResPoints = "7", MinReqNResPoints="5", AvailableLicenses="123", TotalApplications=221, Season="1", Year="2009" },
        };
        return View(_results);
    }
    

    然后:

    @model System.Collections.Generic.List<AppName.Models.GMUOverview>
    @{
        var grid = new WebGrid(Model);
    }
    
    @using (Html.BeginForm())
    {   
        <div id="grid"> 
            @grid.GetHtml() 
        </div> 
    }
    

    【讨论】:

    • 我想使用 ViewModel,但我已经在页面上使用了另一个模型,并从该冲突中得到错误。如果我能过去,我不会使用 ViewBag。谢谢!
    • @KeelRisk,只需为当前模型创建另一个属性以保存 Grid 数据源 List&lt;GMUOverview&gt; 。因此,您可以混合当前模型和数据源。这就是使用 View Model 的原因。仅供您查看,不会中断模型中的其他数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    • 2012-05-13
    • 2015-03-31
    • 2012-09-22
    • 2022-01-03
    • 1970-01-01
    相关资源
    最近更新 更多