C#Razor-根据键显示列表中的值

C#Razor-根据键显示列表中的值,c#,asp.net-mvc-3,razor,C#,Asp.net Mvc 3,Razor,我正试图根据键显示列表中的一些文本: <td>@x.OutcomeSummary</td> <td>@Model.SummaryOutcomes.FirstOrDefault(c => c.Value == x.OutcomeSummary).Name</td> <td>@Model.SummaryOutcomes.FirstOrDefault(x.OutcomeSummary).Name</td> 这是类别类别:

我正试图根据键显示列表中的一些文本:

<td>@x.OutcomeSummary</td>
<td>@Model.SummaryOutcomes.FirstOrDefault(c => c.Value == x.OutcomeSummary).Name</td>
<td>@Model.SummaryOutcomes.FirstOrDefault(x.OutcomeSummary).Name</td>
这是类别类别:

public class Category
{
    public string Value { get; set; }
    public string Name { get; set; }
    public bool InUse { get; set; }
}
这是控制器:

public ActionResult Summary()
{
    var vm = new DogSummaryView
    {
        Incidents = repository.GetAllRecords().OrderByDescending(x => x.DateRecieved),
        SummaryOutcomes = repository.GetAllSummaryOutcomes()
    };

    return View("Summary", vm);
}
最后,您可以看到列表已填充并初始化:

有没有办法得到它,这样它就可以显示正确的摘要结果,而不是显示7

谢谢


非常感谢fourpastmidnight在这方面的不懈帮助,不仅帮助我找到解决方案,而且帮助我准确地理解问题所在。以下是一个更新的(有效的!)解决方案:

@foreach (var x in Model.Incidents)
{
    var summaryOutput = "";
    var firstOutcomeSummary = Model.SummaryOutcomes.FirstOrDefault(c => c.Value == x.OutcomeSummary);
    if (firstOutcomeSummary != null) { summaryOutput = Model.SummaryOutcomes.FirstOrDefault(c => c.Value == x.OutcomeSummary).Name.ToString(); }

     <tr>
        <td>@Html.Raw(summaryOutput)</td>
    </tr>
}
@foreach(Model.incents中的变量x)
{
var summaryOutput=“”;
var firstOutcomeSummary=Model.SummaryOutcomes.FirstOrDefault(c=>c.Value==x.OutcomeSummary);
if(firstOutcomeSummary!=null){summaryOutput=Model.summaryOutlets.FirstOrDefault(c=>c.Value==x.OutcomeSummary).Name.ToString();}
@Html.Raw(汇总输出)
}
FirstOrDefault只返回与谓词匹配的第一个元素或返回默认值,即null。对空对象调用.Name将引发NullReferenceException

总之,如果枚举SummaryOutlets中没有值为7的Category实例,则将发生您描述的行为。

FirstOrDefault将只返回与谓词匹配的第一个元素或返回默认值,即null。对空对象调用.Name将引发NullReferenceException

总之,如果枚举SummaryOutlets中没有值为7的Category实例,则将发生您描述的行为。

FirstOrDefault将只返回与谓词匹配的第一个元素或返回默认值,即null。对空对象调用.Name将引发NullReferenceException

总之,如果枚举SummaryOutlets中没有值为7的Category实例,则将发生您描述的行为。

FirstOrDefault将只返回与谓词匹配的第一个元素或返回默认值,即null。对空对象调用.Name将引发NullReferenceException


总之,如果枚举汇总结果中没有值为7的Category实例,则将发生您描述的行为。

好的,问题是您试图将
字符串
int
进行比较

将第二行更改如下:

@Model.SummaryOutcomes.FirstOrDefault(c => c.Value == x.OutcomeSummary.ToString()).Name;

// You could also use '.Value'.
那应该能解决你的问题

更新

嗯,可能是
x.outcomessummary.ToString()
导致的是枚举的类型名,而不是枚举常量值的整数值

尝试将上述行更新为以下内容:

@Model.SummaryOutcomes.FirstOrDefault(c => c.Value == ((int)x.OutcomeSummary).ToString()).Name;
// If x.OutcomeSummary is the outcome summary name, then....
var firstOutcomeSummary = @Model.SummaryOutcomes.FirstOrDefault(c => c.Name == x.OutcomeSummary)
if (firstOutcomeSummary != null)
    // Do something here.

// Else, if x.OutcomeSummary is the outcome summary value, e.g. "7", then...
var firstOutcomeSummary = @Model.SummaryOutcomes.FirstOrDefault(c => c.Value == x.OutcomeSummary)
if (firstOutcomeSummary != null)
    // Do something here.
更新2014-03-21

根据OP的最新评论,尝试以下方法:

@Model.SummaryOutcomes.FirstOrDefault(c => c.Value == ((int)x.OutcomeSummary).ToString()).Name;
// If x.OutcomeSummary is the outcome summary name, then....
var firstOutcomeSummary = @Model.SummaryOutcomes.FirstOrDefault(c => c.Name == x.OutcomeSummary)
if (firstOutcomeSummary != null)
    // Do something here.

// Else, if x.OutcomeSummary is the outcome summary value, e.g. "7", then...
var firstOutcomeSummary = @Model.SummaryOutcomes.FirstOrDefault(c => c.Value == x.OutcomeSummary)
if (firstOutcomeSummary != null)
    // Do something here.

好的,问题是您试图将
字符串
int
进行比较

将第二行更改如下:

@Model.SummaryOutcomes.FirstOrDefault(c => c.Value == x.OutcomeSummary.ToString()).Name;

// You could also use '.Value'.
那应该能解决你的问题

更新

嗯,可能是
x.outcomessummary.ToString()
导致的是枚举的类型名,而不是枚举常量值的整数值

尝试将上述行更新为以下内容:

@Model.SummaryOutcomes.FirstOrDefault(c => c.Value == ((int)x.OutcomeSummary).ToString()).Name;
// If x.OutcomeSummary is the outcome summary name, then....
var firstOutcomeSummary = @Model.SummaryOutcomes.FirstOrDefault(c => c.Name == x.OutcomeSummary)
if (firstOutcomeSummary != null)
    // Do something here.

// Else, if x.OutcomeSummary is the outcome summary value, e.g. "7", then...
var firstOutcomeSummary = @Model.SummaryOutcomes.FirstOrDefault(c => c.Value == x.OutcomeSummary)
if (firstOutcomeSummary != null)
    // Do something here.
更新2014-03-21

根据OP的最新评论,尝试以下方法:

@Model.SummaryOutcomes.FirstOrDefault(c => c.Value == ((int)x.OutcomeSummary).ToString()).Name;
// If x.OutcomeSummary is the outcome summary name, then....
var firstOutcomeSummary = @Model.SummaryOutcomes.FirstOrDefault(c => c.Name == x.OutcomeSummary)
if (firstOutcomeSummary != null)
    // Do something here.

// Else, if x.OutcomeSummary is the outcome summary value, e.g. "7", then...
var firstOutcomeSummary = @Model.SummaryOutcomes.FirstOrDefault(c => c.Value == x.OutcomeSummary)
if (firstOutcomeSummary != null)
    // Do something here.

好的,问题是您试图将
字符串
int
进行比较

将第二行更改如下:

@Model.SummaryOutcomes.FirstOrDefault(c => c.Value == x.OutcomeSummary.ToString()).Name;

// You could also use '.Value'.
那应该能解决你的问题

更新

嗯,可能是
x.outcomessummary.ToString()
导致的是枚举的类型名,而不是枚举常量值的整数值

尝试将上述行更新为以下内容:

@Model.SummaryOutcomes.FirstOrDefault(c => c.Value == ((int)x.OutcomeSummary).ToString()).Name;
// If x.OutcomeSummary is the outcome summary name, then....
var firstOutcomeSummary = @Model.SummaryOutcomes.FirstOrDefault(c => c.Name == x.OutcomeSummary)
if (firstOutcomeSummary != null)
    // Do something here.

// Else, if x.OutcomeSummary is the outcome summary value, e.g. "7", then...
var firstOutcomeSummary = @Model.SummaryOutcomes.FirstOrDefault(c => c.Value == x.OutcomeSummary)
if (firstOutcomeSummary != null)
    // Do something here.
更新2014-03-21

根据OP的最新评论,尝试以下方法:

@Model.SummaryOutcomes.FirstOrDefault(c => c.Value == ((int)x.OutcomeSummary).ToString()).Name;
// If x.OutcomeSummary is the outcome summary name, then....
var firstOutcomeSummary = @Model.SummaryOutcomes.FirstOrDefault(c => c.Name == x.OutcomeSummary)
if (firstOutcomeSummary != null)
    // Do something here.

// Else, if x.OutcomeSummary is the outcome summary value, e.g. "7", then...
var firstOutcomeSummary = @Model.SummaryOutcomes.FirstOrDefault(c => c.Value == x.OutcomeSummary)
if (firstOutcomeSummary != null)
    // Do something here.

好的,问题是您试图将
字符串
int
进行比较

将第二行更改如下:

@Model.SummaryOutcomes.FirstOrDefault(c => c.Value == x.OutcomeSummary.ToString()).Name;

// You could also use '.Value'.
那应该能解决你的问题

更新

嗯,可能是
x.outcomessummary.ToString()
导致的是枚举的类型名,而不是枚举常量值的整数值

尝试将上述行更新为以下内容:

@Model.SummaryOutcomes.FirstOrDefault(c => c.Value == ((int)x.OutcomeSummary).ToString()).Name;
// If x.OutcomeSummary is the outcome summary name, then....
var firstOutcomeSummary = @Model.SummaryOutcomes.FirstOrDefault(c => c.Name == x.OutcomeSummary)
if (firstOutcomeSummary != null)
    // Do something here.

// Else, if x.OutcomeSummary is the outcome summary value, e.g. "7", then...
var firstOutcomeSummary = @Model.SummaryOutcomes.FirstOrDefault(c => c.Value == x.OutcomeSummary)
if (firstOutcomeSummary != null)
    // Do something here.
更新2014-03-21

根据OP的最新评论,尝试以下方法:

@Model.SummaryOutcomes.FirstOrDefault(c => c.Value == ((int)x.OutcomeSummary).ToString()).Name;
// If x.OutcomeSummary is the outcome summary name, then....
var firstOutcomeSummary = @Model.SummaryOutcomes.FirstOrDefault(c => c.Name == x.OutcomeSummary)
if (firstOutcomeSummary != null)
    // Do something here.

// Else, if x.OutcomeSummary is the outcome summary value, e.g. "7", then...
var firstOutcomeSummary = @Model.SummaryOutcomes.FirstOrDefault(c => c.Value == x.OutcomeSummary)
if (firstOutcomeSummary != null)
    // Do something here.


你初始化了总结结果了吗?你能发布你的总结结果吗controller@Nick请参阅下面的@Rui答案。这是最有可能的原因。至于
c
y
,这并不重要。这只是匿名函数的lambda变量。我更喜欢使用1个字符的lambda变量,这些变量与lambda操作的类型的第一个字符相匹配。由于
summaryoutcouts
是一个
IEnumerable
,我为
Category
选择了
c
;e、 g.
Enum.Getname(typeof(summaryoutput),summaryoutput.WhateverThisValueIs)
@Nick:我将对
if
语句的代码进行如下重构:
if(firstOutcomeSummary!=null){/*(这里新行)*/@firstOutcomeSummary.Name/*(这里新行)*/}。由于
firstOutcomeSummary`不为空,因此可以直接访问
Name
属性。另外,我不会使用
@Html.Raw()
——这可能会导致脚本注入攻击,除非您仔细清理用户输入;即便如此,除非绝对必要,我还是不会这么做。你初始化总结结果了吗?你能发布你的结果吗controller@Nick请参阅下面的@Rui答案。这是最有可能的原因。至于
c
y
,这并不重要。这只是匿名函数的lambda变量。我更喜欢使用1个字符且与第一个字符匹配的lambda变量