如何将List查询结果转换为查看模型?

时间:2017-05-08 16:23:22

标签: asp.net-mvc types

我有一个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()
}

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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)
}

这种方法适用于将单个用户配置文件数据传递给视图,这是我在这种情况下所需要的。还可以获得上述链接的信用。谢谢!