【问题标题】:How do you convert a List query result to view model?如何将 List 查询结果转换为视图模型?
【发布时间】:2017-05-10 00:05:52
【问题描述】:

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

【问题讨论】:

    标签: asp.net-mvc types


    【解决方案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>
    

    【讨论】:

    【解决方案2】:

    上述答案适用于数据部分。我将补充一点,就我正在做的事情而言,我可以通过在我的控制器中执行以下操作来实现它:

    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)
    }
    

    这种方法适用于将单个用户配置文件数据传递给视图,这正是我在这种情况下所需要的。也对上述链接表示感谢。谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-22
      • 1970-01-01
      • 1970-01-01
      • 2011-02-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-23
      • 1970-01-01
      相关资源
      最近更新 更多