How to return a DataSet to a View

前端 未结 4 460
天命终不由人
天命终不由人 2021-01-12 07:55

I am sending a standard Sql select statement to my Sql box via the SqlDataAdapter, then populating a DataSet object.

I can access the rows in the resulting DataSet,

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-12 08:31

    Controller code

     //pQ is your query you have created 
     //P4DAL is the key name for connection string 
    
      DataSet ds = pQ.Execute(System.Configuration.ConfigurationManager.ConnectionStrings["Platform4"].ConnectionString);
    
    
      //ds will be used below
      //create your own view model according to what you want in your view 
     //VMData is my view model
    
     var _buildList = new List();
                {
                    foreach (DataRow _row in ds.Tables[0].Rows)
                    {
                        _buildList.Add(new VMData
                        {  
         //chose what you want from the dataset results and assign it your view model fields 
    
                            clientID = Convert.ToInt16(_row[1]),
                            ClientName = _row[3].ToString(),
                            clientPhone = _row[4].ToString(),
                            bcName = _row[8].ToString(),
                            cityName = _row[5].ToString(),
                            provName = _row[6].ToString(),
                        });
    
    
    
                    }
    
                }
    
      //you will use this in your view
      ViewData["MyData"] = _buildList;
    

    View

    @if (ViewData["MyData"] != null)
    {
    
        var data = (List)ViewData["MyData"];
        
    @foreach (var item in data) { }
    Name Telephone Category City Province
    @Html.ActionLink(item.ClientName, "_Display", new { id = item.clientID }, new { target = "_blank" }) @item.clientPhone @item.bcName @item.cityName @item.provName
    }

提交回复
热议问题