Asp.net mvc ViewData显示数据库列名

Asp.net mvc ViewData显示数据库列名,asp.net-mvc,linq-to-sql,viewdata,Asp.net Mvc,Linq To Sql,Viewdata,我尝试将一些数据输入名为“成本”的ViewData。这是从数据库中的“PrinterCreditCost”列中获取一个值,并应显示该列的值。但是,列名也用花括号显示,因此与仅显示“2”不同,它显示“{PrinterCreditCost=2}”。有人知道我怎样才能拥有它,使它只显示“2”吗。这将用于一个数学方程,所以我只需要2 代码如下: var result = (from a in db.tblOptions.Include(t => t.PrinterCreditCost) where

我尝试将一些数据输入名为“成本”的
ViewData
。这是从数据库中的“PrinterCreditCost”列中获取一个值,并应显示该列的值。但是,列名也用花括号显示,因此与仅显示“2”不同,它显示“{PrinterCreditCost=2}”。有人知道我怎样才能拥有它,使它只显示“2”吗。这将用于一个数学方程,所以我只需要2

代码如下:

var result = (from a in db.tblOptions.Include(t => t.PrinterCreditCost)
where a.ID == 1
select new
{
    a.PrinterCreditCost
}
).First();
ViewData["Cost"] = result;
Jason的更新:

        public int ID { get; set; }
    public int Visible { get; set; }
    public int DeleteLicense { get; set; }
    public Nullable<int> ICTSurvey { get; set; }
    public Nullable<int> PaidWorkSurvey { get; set; }
    public Nullable<int> ICTGeneralSurvey { get; set; }
    public Nullable<int> PESSCLSurvey { get; set; }
    public Nullable<int> PastSurvey { get; set; }
    public Nullable<int> PresentSurvey { get; set; }
    public Nullable<int> Yr11Survey { get; set; }
    public Nullable<int> NewScientistCount { get; set; }
    public Nullable<int> UCASYear { get; set; }
    public Nullable<int> OnlineSurveyActive { get; set; }
    public Nullable<int> OnlineExaminationsActive { get; set; }
    public string UCASFirstHalf { get; set; }
    public string UCASSecondHalf { get; set; }
    public string SIMSManual { get; set; }
    public string SIMSApplication { get; set; }
    public string SIMSAmpark { get; set; }
    public Nullable<double> PrinterCreditFund { get; set; }
    public Nullable<int> PrinterCreditCost { get; set; }
    public string UCASIndividualFirstHalf { get; set; }
    public string UCASIndividualSecondHalf { get; set; }
    public string BookingSheetYear { get; set; }
    public Nullable<System.DateTime> InductionDate { get; set; }
    public string InductionYear { get; set; }

    public virtual ICollection<tblPrinterCredit> tblPrinterCredits { get; set; }
public int ID{get;set;}
公共int可见{get;set;}
public int DeleteLicense{get;set;}
可为空的公共属性{get;set;}
公共可为空的PaidWorkSurvey{get;set;}
公共可空ICTGeneralSurvey{get;set;}
可为空的公共数据集{get;set;}
公共可为空的PastSurvey{get;set;}
公共可为空的PresentSurvey{get;set;}
公共可为空的YR111Survey{get;set;}
公共可为空的NewScientistCount{get;set;}
公共可空UCASYear{get;set;}
公共可空的OnlineSurveyActive{get;set;}
公共可空的联机检查活动{get;set;}
公共字符串UCASFirstHalf{get;set;}
公共字符串UCASSecondHalf{get;set;}
公共字符串{get;set;}
公共字符串应用程序{get;set;}
公共字符串SIMSAmpark{get;set;}
公共可为空的PrinterCreditFund{get;set;}
公共可为空的PrinterCreditCost{get;set;}
公共字符串UCASIndividualFirstHalf{get;set;}
公共字符串UCASIndividualSecondHalf{get;set;}
公共字符串BookingSheetYear{get;set;}
公共可为空的归纳日期{get;set;}
公共字符串归纳EAR{get;set;}
公共虚拟ICollection tblPrinterCredits{get;set;}

一种可能的方法是将结果存储在字符串中,然后选择存储字符串的子字符串并将其分配给查看数据

  string abc = result;
  string viewdatastring = abc.substring(20);
  ViewData[Cost]= viewdatastring
请看这里的子字符串教程


在查询的这一部分

select new
{
    a.PrinterCreditCost
}
是否有需要使用的
PrinterCreditCost
属性,例如

select new
{
    a.PrinterCreditCost.Value
}
我不知道
Value
是否是您需要的属性,它可能被称为其他属性

目前看来,您的查询确实返回的是列定义,而不是该列的值

编辑:

我看到以下情况:

public可空PrinterCreditCost{get;set;}

所以我很惊讶

select new
{
    a.PrinterCreditCost.Value
}
不起作用。由于它是一个可为空的值,
.value
属性应该返回该值,而不是
{value=2}
。我不知道那里发生了什么事

var result = (from a in db.tblOptions.Include(t => t.PrinterCreditCost)
where a.ID == 1
select new
{
    a.PrinterCreditCost
}
).First();
ViewData["Cost"] = result;
但是,将其更改为:

    var result = (from a in db.tblOptions.Include(t => t.PrinterCreditCost)
where a.ID == 1
select new
{
    a.PrinterCreditCost
}
).First();
ViewData["Cost"] = result.Value;

(添加.Value)到ViewData行的末尾将起作用。

不幸的是,它是一个int而不是字符串,因此不能使用字符串。使用Tostring方法将int转换为字符串,如果需要,还可以使用int.parse方法将int转换回int。你可以通过谷歌搜索获得大量的例子。我们将研究:)。谢谢谢谢如果堆栈溢出成员提供的任何解决方案对您有用,请向上投票该解决方案。如果该解决方案有效,请单击离答案较近的勾号接受该解决方案。再次感谢。因此,我尝试使用“.ToString()”而不是。首先在查询结束时,然后,作为测试,尝试使用String sub=result;字符串子字符串=子字符串(4)。这会将[dbo].[tblOptions]中的[Extent1].[ID]作为[ID],[Extent1].[PrinterCreditCost]作为[PrinterCreditCost]作为[Extent1]返回,其中1=[Extent1].[ID]与任何实际结果相反。在我看来,它似乎在尝试对Linq查询进行子串,而不是结果?尝试添加.Value,它给出了类似的结果。它不是“PrinterCreditCost=2”,而是“Value=2”。您可以发布类的代码吗
PrinterCreditCost
请?我将用它更新问题。对于这个特定的控制器来说,无论它值多少钱,它都是完全冗余的。该表与许多其他表等相关。将其更改为ViewData[“Cost”]=result;要查看数据[“成本”]=result.Value有效:)嗯,我没想到会这样,但我很高兴它现在对您有效:)