【问题标题】:View with more than 3 table data in MVC3在 MVC3 中查看超过 3 个表数据
【发布时间】:2012-06-12 18:48:35
【问题描述】:

我正在使用 MVC3 构建一个 Intranet 网站。我有一个要显示 3 个表格的视图。仅从一个数据库中获取所有 3 个表数据。对于 3 个表数据,我有 3 个不同的存储过程。我正在考虑在 index.cshtml 中使用 3 个不同的局部视图。并且可以使用3个存储过程来获取数据。

谁能帮助我如何设置控制器索引方法,以便它返回所有 3 个局部视图的视图?我可以使用viewbag。但是我在网上阅读了一些文章,它说这不是使用 viewbag 的好方法。目前,控制器中的 Index 方法使用 1 个存储过程仅返回一个部分视图的视图。

提前致谢。

【问题讨论】:

    标签: asp.net-mvc-3 sql-server-2008


    【解决方案1】:

    创建自定义视图模型。

    型号:

    public class MyViewModel 
    {
        public DataSet Result1 { get; set; }
        public DataSet Result2 { get; set; }
        public DataSet Result3 { get; set; }
    }
    

    控制器动作:

    public ViewResult myAction()
    {
        var myViewModel = new MyViewModel({
             Result1 = XXX, // get result 1
             Result2 = XXX, // get result 2
             Result3 = XXX  // get result 3
        });
    
        return View(myViewModel);
    }
    

    查看:

    @model MyViewModel;
    
    <div id="results1">@Model.Result1.Tables[0].Rows[0]["someValueFromDB"].ToString();</div>
    ... etc.
    

    【讨论】:

    • 这有帮助,但我试图在视图中使用 foreach 循环,其中显示“foreach 语句无法对 'PagedList.IPagedList' 类型的变量进行操作,因为 'PagedList.IPagedList' 不包含公共'GetEnumerator' 的定义"
    • 已修复。我正在对从存储过程中提取的数据使用分页。更新了视图模型类。公共类 MyViewModel { publicc PagedList.IPagedList results1 {get;设置;} //等等 }。我以前只使用 PagedList 。谢谢!!
    猜你喜欢
    • 2018-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多