开发者

How do you render 2 or more DataSets with .net MVC

I have 3 DataSets that I would like to render in a view. I have seen several examples to render 开发者_运维问答one, but not two.

It is possible to put the DataSets in the ViewData Collection but then they are not "DataSets" any more when accessing them in the veiw.

Thanks.


ViewData is a bit hacky and easily abused.

You should create a ViewModel and place them there. Fill the Model in the Controller, then pass it to the View.

MODEL

public class MyPageModel
{
    public DataSet data1 { get; set; }
    public DataSet data2 { get; set; }
}

VIEW

<%@ Page Title="" Language="C#" MasterPageFile="~/whatever.master" Inherits="System.Web.Mvc.ViewPage<MyNamespace.MyPageModel>" %>

<%: foreach (var row in Model.data1.Tables[0].Rows) { blah blah blah } %>
<%: foreach (var row in Model.data2.Tables[0].Rows) { blah blah blah } %>

CONTROLLER

public ActionResult MyPage()
{
    var model = new MyPageModel();

    // Fill model.data1 here....
    // Fill model.data2 here....

    return View(model);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜