所以我有点卡住了。我还在学习所有这些东西。但是我必须在我的应用程序中添加一个csv解析器,它应该在我的警报页面上显示结果。如果我做
return PartialView(model, pin, tDate, stat);
它会告诉我pin,tDate和stat在当前上下文中不存在。如果我将它们取出,应用程序将运行,但不会显示预期的结果。
我在UserADInfoModel.cs
中声明了pin,tDate和stat这是控制器:
public ActionResult _Alerts(UserADInfoModel model, List<string> groups)
{
DataRowCollection drColl = Core.GetAlerts(model);
ViewBag.Alerts = drColl;
var path = @"Exchange Migration data 10-25-17.csv";
using (TextFieldParser csvParser = new TextFieldParser(path))
{
csvParser.CommentTokens = new string[] { "#" };
csvParser.SetDelimiters(new string[] { "," });
csvParser.HasFieldsEnclosedInQuotes = true;
// Skip the row with the column names
csvParser.ReadLine();
// Read the lines
while (!csvParser.EndOfData)
{
string[] fields = csvParser.ReadFields();
string pin = fields[0];
string tDate = fields[2];
string stat = fields[6];
}
}
return PartialView(model, pin, tDate, stat);
}
这是视图
@if (@ViewBag.pin == Model.SAM)
{
<tr style="background-color : #ff3333; color: #ffffff">
<td style="padding-left :10px; padding-right: 10px;padding-top:2px; padding-bottom: 2px">
<p>Critical</p>
</td>
<td style="padding-left :10px; padding-right: 10px;padding-top:2px; padding-bottom: 2px">
<p>Exchange Migration</p>
</td>
<td style="padding-left :10px; padding-right: 10px;padding-top:2px; padding-bottom: 2px">
<p>Caller was set to migrate on (@ViewBag.tDate). The status of the migration is (@ViewBag.stat). Please contact Hypercare</p>
</td>
</tr>
}
@foreach (var x in ViewBag.Alerts)
{
var uClass = (x["Weight"].Contains("Warning")) ? "#ff8c1a, " : (x["Weight"].Contains("Critical")) ? "#ff3333" : "";
<tr @if (x["Weight"].Contains("Warning")) {
@MvcHtmlString.Create("style=\"background-color: #ff8c1a\"")
}
else if(x["Weight"].Contains("Critical")){
@MvcHtmlString.Create("style=\"background-color: #ff3333; color: #ffffff\"")
}>
我做错了什么? TIA
答案 0 :(得分:0)
您在pin
范围内声明了tDate
,stat
和while
。
决定是否要使用模型(推荐)或ViewBag
传递数据:
public ActionResult _Alerts(UserADInfoModel model, List<string> groups)
{
DataRowCollection drColl = Core.GetAlerts(model);
ViewBag.Alerts = drColl;
var path = @"Exchange Migration data 10-25-17.csv";
using (TextFieldParser csvParser = new TextFieldParser(path))
{
// ...
while (!csvParser.EndOfData)
{
string[] fields = csvParser.ReadFields();
model.pin = fields[0];
model.tDate = fields[2];
model.stat = fields[6];
}
}
return PartialView(model);
}
_Alerts.cshtml:
@model UserADInfoModel
@if (@Model.pin == Model.SAM)
// ...
// ... @Model.tDate ... @Model.stat ...