本文介绍了如何在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;
}
}
}
推荐答案
这篇关于如何在C#中从动态模型创建MVC视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!