如何在C#中从动态模型创建MVC视图 [英] How to create MVC view from a dynamic model in C#

查看:206
本文介绍了如何在C#中从动态模型创建MVC视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have a stored sql server getting a dynamic pivot from table 
now i want to display this in a view in mvc
i using asp.net and MVC5
thanks for all.





我尝试过:



i尝试下面的代码





What I have tried:

i tried code in below

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YMOReporting.Models;
using System.Data.SqlClient;
 
namespace YMOReporting.Controllers.Reportings
{
    public class Proc1Controller : Controller
    {
        private YMOReportingConnex db = new YMOReportingConnex();
 
            // GET: Proc1 : stored for a dynamic pivot
            public ActionResult Index()
            {
                return View();
            }
        public DataTable PivotTableView()
        {
            db.GetProc1().ToList();
 
            DataTable dt = new DataTable();
 
            //GetProc1() return All data for Table.
            var data = db.GetProc1().ToList();
 
            //Applying linq for geeting pivot output
            var d = (from f in data
                     group f by new { f.Planner, f.Vendor_Location, f.Bulk_Material }
                into myGroup
                     where myGroup.Count() > 0
                     select new
                     {
                         myGroup.Key.Planner,
                         myGroup.Key.Vendor_Location,
                         myGroup.Key.Bulk_Material,
                         subject = myGroup.GroupBy(f => f.Week).Select
                             (m => new { Sub = m.Key, Value = m.Sum(c => c.Value) })
                     }).ToList();
 
            var sub = db.GetProc1().ToList();
            // Distinct Week Like Below
            //Creating array for adding dynamic columns
            ArrayList objDataColumn = new ArrayList();
 
            if (data.Count() > 0)
            {
                //Three column are fix "Planner","Vendor_Location","Bulk_Material".
                objDataColumn.Add("Planner");
                objDataColumn.Add("Vendor_Location");
                objDataColumn.Add("Bulk_Material");
 
                //Add Subject Name as column in Datatable
                for (int i = 0; i < sub.Count; i++)
                {
                    objDataColumn.Add(sub[i].Week);
                }
 
                //Add dynamic columns name to datatable dt
                for (int i = 0; i < objDataColumn.Count; i++)
                {
                    dt.Columns.Add(objDataColumn[i].ToString());
                }
 
                //Add data into datatable with respect to dynamic columns and dynamic data
                for (int i = 0; i < d.Count; i++)
                {
                    List<string> tempList = new List<string>();
                    tempList.Add(d[i].Planner.ToString());
                    tempList.Add(d[i].Vendor_Location.ToString());
                    tempList.Add(d[i].Bulk_Material.ToString());
 
                    var res = d[i].subject.ToList();
                    for (int j = 0; j < res.Count; j++)
                    {
                        tempList.Add(res[j].Value.ToString());
                    }
 
                    dt.Rows.Add(tempList.ToArray<string>());
                }
 
            }
            return dt;
 
        }
 
    }
}

推荐答案

我google了 mvc view show datatable这是第一个结果



https://www.aspsnippets.com/Articles/Display-Show-DataSets-and-DataTables-in-View-in-ASPNet-MVC -Razor.aspx [ ^ ]



如果这不符合您的需求,请谷歌自己寻找更多信息。请注意,如何填充数据表并不重要,因此不要忽略不涉及数据透视表的结果。
I googled "mvc view show datatable" and this was the first result

https://www.aspsnippets.com/Articles/Display-Show-DataSets-and-DataTables-in-View-in-ASPNet-MVC-Razor.aspx[^]

If that doesn't suit your needs then google yourself to find more. Note it doesn't matter how the datatable is populated so don't dismiss results that don't involve pivot tables.


这篇关于如何在C#中从动态模型创建MVC视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆