2017-05-08 81 views
0

我有一个MVC控制器方法List<DataRow> GetUserCriteria()它运行标准的SQL查询,没有EF或自动映射器。运行后查询它:如何将列表查询结果转换为查看模型?

DataTable dt = new DataTable(); 
SqlDataAdapter sdp = new SqlDataAdapter(objCommand) 
conn.Open(); 
sdp.Fill(dt) 
list = dt.AsEnumerable().ToList(); 

return list; 

的问题是在我的ActionResultMethod返回相关的观点,我怎么能说列表转换为正确的类型为视图模型消耗,并在视图中使用?

public ActionResult ClientProfile() { 

    var rawData = GetUserCriteria(); 
    //Convert to type for view model here and pass into the view below. 

return View() 
} 

回答

1

您可以避免转换,使之简单:

public class aDataTableView 
{ 
    public DataTable aTable { get; set; } 
} 

public class HomeController : Controller 
{ 
    //use any action or code that goes here 
    public ActionResult Index63() 
    { 
     DataTable dt = new DataTable(); 
     SqlConnection conn = new SqlConnection(@"data source=.\sqlexpress;initial catalog=Breaz;integrated security=True"); 
     //conn.Open(); 
     SqlCommand objCommand = new SqlCommand("Select * from dbo.Example", conn); 
     SqlDataAdapter sdp = new SqlDataAdapter(objCommand); 
     sdp.Fill(dt); 

     //you are not explicitely disposing of dt 
     //dt.Dispose(); 

     aDataTableView dtw = new aDataTableView { aTable = dt }; 

     objCommand.Dispose(); 
     sdp.Dispose(); 
     conn.Close(); 
     conn.Dispose(); 

     return View(dtw); 
    } 

@model Testy20161006.Controllers.aDataTableView 

@{ 
     Layout = null; 
} 

<!DOCTYPE html> 

<html> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title>Index63</title> 

</head> 
<body> 
    @using (Html.BeginForm()) 
     { 
     <table border="1"> 
      <thead> 
       <tr> 
        @foreach (System.Data.DataColumn col in Model.aTable.Columns) { 
       <th>@col.Caption</th> 
      } 
       </tr> 
      </thead> 
      <tbody> 
       @foreach(System.Data.DataRow row in Model.aTable.Rows) { 
       <tr> 
        @foreach (var cell in row.ItemArray) { 
        <td>@cell.ToString() </td> 
        } 
       </tr> 
       } 
      </tbody> 
     </table> 
     <input type="submit" value="click" /> 
     } 
</body> 
</html> 
+0

信用http://stackoverflow.com/questions/2243898/displaying-standard-datatables-in-mvc – kblau

+0

是否有一个在窗体中呈现此示例?我传递给视图的数据是用HtmlHelpers呈现的用户配置文件数据 – LifeOf0sAnd1s

0

以上回答就职于数据部分。我只想补充一点,我在做什么的情况下,我能够做我的控制下实现它:

public ActionResult Index63() { 
DataTable dt = new DataTable(); 
     SqlConnection conn = new SqlConnection(@"data source=.\sqlexpress;initial catalog=Breaz;integrated security=True"); 
     //conn.Open(); 
     SqlCommand objCommand = new SqlCommand("Select * from dbo.Example", conn); 
     SqlDataAdapter sdp = new SqlDataAdapter(objCommand); 
     sdp.Fill(dt); 

     aDataTableView dtw = new aDataTableView { aTable = dt }; 

     //Cast Each Row Element to an object 
     object FirstNameField = dt.Row[0][3] 

     //Map user values to model backing view 
     Index63ViewModel userViewModel = new Index63ViewModel(); 

     userViewModel.FirstName = FirstNameField.ToString(); 

return(userViewModel) 
} 

这种方法适用于通过一个单一的用户配置文件数据,这是视图在这种情况下我需要什么。上面的链接也提供了信用。谢谢!