eyalb
eyalb

Reputation: 3022

what type of data i need to pass from controller to view?

i need to pass data to my view.
my ActionResult is this

public ActionResult Index()
    {
        SiteMapData sp = new SiteMapData();
        ViewData["folders"] = sp.GetFolders();
        return View();
    }

sp.GetFolders returns a DataView

internal DataView GetFolders()
    {
        SqlCommand cmd = new SqlCommand("usp_PGF_Select");
        cmd.CommandType = CommandType.StoredProcedure;

        SqlConnection myConnection = Util.GetConnection();
        cmd.Connection = myConnection;
        SqlDataAdapter da = new SqlDataAdapter(cmd);

        DataSet ds = new DataSet();
        da.Fill(ds);
        DataView dView = ds.Tables[0].DefaultView;
        return dView;
    }

how i need to go over the data in my view.
i try this but it's not work

@foreach (var c in (System.Data.DataView)ViewData["folders"])
{
    <p>@c.SMF_Name</p>
}

maybe i need to pass another type of data?

Upvotes: 0

Views: 190

Answers (1)

Paul Creasey
Paul Creasey

Reputation: 28864

You should be doing object relational mapping (ORM), which mean creating classes that represent your database structure and mapping the results to those. It is possible to do this manually of course, but a could ORM is strongly recomended!

Try LINQ to SQL to start with, or perhaps Entity Framework, there are loads of good guides out there.

You will then be returning something of type System.Collections.Generic.List<MyTable> which is much much easier to work with.

Upvotes: 1

Related Questions