我花了很多时间在这上面,但我似乎无法让它发挥作用。我有一个遗留的Web表单应用程序,我在MVC中重写。它具有来自存储过程的所有业务逻辑,并且在时间的迭代中,我决定重用BAL(业务层)和DAL。这就是我所拥有的:
模型
public class GridDisplay
{
public List<string> columnNames { get; set; }
public List<string> fieldValues { get; set; }
}
控制器
public ActionResult PatientInfo()
{
var model = new PatientInfoViewmodel();
ViewBag.countries = model.FillCountryDropDown();
ViewBag.studies = model.FillStudyDropDown();
SearchElements SE = new SearchElements { StudyID = "PRMDEMO" };
List<string> fields = new List<string>();
DataSet ds = new DataSet();
ds = pibl.PopulateData(SE);
var display = new List<GridDisplay>();
display = model.BuildGridData(SE);
ViewBag.columns = display[0].columnNames.ToList();
ViewBag.fields = display;
return View();
}
GridDisplays列表:
public List<GridDisplay> BuildGridData(SearchElements SE) //(string country, string studyID, string patientid, string lastname)
{
DataSet ds = new DataSet();`enter code here`
ds = pibl.PopulateData(SE);
GridDisplay display = new GridDisplay();
List<GridDisplay> displayList = new List<GridDisplay>();
string strtemp = string.Empty;
display.columnNames = new List<string>();
foreach (DataRow row in ds.Tables[0].Rows)
{
display.columnNames.Add(row["label"].ToString());
}
display.fieldValues = new List<string>();
foreach (DataRow row in ds.Tables[1].Rows)
{
display.fieldValues = new List<string>();
foreach (DataColumn column in ds.Tables[1].Columns)
{
if (column.Caption != "STUDY" & column.Caption != "PATIENT_ID")
{
display.fieldValues.Add(row[column].ToString());
}
if (column.Caption == "PATIENT_ID")
{
strtemp = row[column].ToString();
}
}
display.fieldValues.Add(strtemp);
displayList.Add(new GridDisplay() { columnNames = display.columnNames, fieldValues = display.fieldValues});
}
return displayList;
}
displayList拥有我需要的一切,我能够显示列,但不能显示字段。以下是与此相关的视图部分(我的一个版本)
<div id="dvDynamicPatient" style="overflow: auto;">
<table id="gvDynamicPatient">
<tr>
@foreach (var col in @ViewBag.columns)
{
<th id="gridHeader" style=" white-space: nowrap;">
@col
</th>
}
</tr>
<tr>
@foreach (var item in @ViewBag.fields.fieldvalues)
{
<td id="gridCell" style="border-style: solid; color: #000000; white-space: nowrap">
@item
</td>
}
</tr>
</table>
</div
答案 0 :(得分:0)
理解模型是一件美好的事情。我确实搞清楚了。回顾一下,我有一个存储过程,它通过数据集返回两个结果集。 Dataset.Tables [0]有头字符串,Dataset.Tables [1]包含要显示的信息。我的任务是创建一个MVC 4应用程序来查看这个,然后是一些。
模型
public class GridDisplay
{
public List<string> columnNames { get; set; }
public List<List<string>> fieldValues { get; set; }
}
控制器
public ActionResult PatientInfo()
{
var model = new PatientInfoViewmodel();
// build dropdown lists
ViewBag.countries = model.FillCountryDropDown();
ViewBag.studies = model.FillStudyDropDown();
SearchElements SE = new SearchElements { StudyID = "PRMDEMO" };
DataSet ds = new DataSet();
ds = pibl.PopulateData(SE);
var gridDisplay = new GridDisplay();
gridDisplay = model.BuildGridData(SE);
return View(gridDisplay);
}
查看
<div id="dvDynamicPatient" style="overflow: auto;">
<table id="gvDynamicPatient">
<tr>
@foreach (var col in Model.columnNames)
{
<th id="gridHeader" style="white-space: nowrap;">
@col
</th>
}
</tr>
@for (int i = 0; i < Model.fieldValues.Count; ++i)
{
<tr id="row" style="white-space: nowrap;">
@foreach (var item in Model.fieldValues[i])
{
<td id="col" style="white-space: nowrap;">
@item
</td>
}
</tr>
}
</table>
</div>