C#MVC - 将字符串转换为变量

时间:2014-09-09 20:29:22

标签: c# asp.net-mvc asp.net-mvc-4

如何将字符串转换为变量?我有一个字符串列表,我希望能够循环并从我的模型中调用数据。

我的代码:

控制器:

List<string> reportContentsCharts = new List<string>();
//Pseudo Code
//If chart is selected added it to reportContents. So in the view instead of calling @Model.getChart1 from the view I can reference this reportContents. 
//For Example:If chart1 and chart2 are selected

reportContentsCharts.Add("getChart1");
reportContentsCharts.Add("getChart2");

IndexViewModel viewModel = new IndexViewModel()
{
      chart1 = makeChart1(DictionaryofData), //Returns an image and sends to IndexViewModel
      chart2 = makeChart2(DictionaryofData),
      chart3 = makeChart2(DictionaryofData),
      chart4 = makeChart2(DictionaryofData),
      chart5 = makeChart2(DictionaryofData),
      chart6 = makeChart2(DictionaryofData),
      reportContentsCharts = reportContentsCharts
}

 private byte[] makeChart1(Dictionary<string, Double> DictionaryofData)
 {
      //code to construct chart and return as an image. 
 }

IndexViewModel:

public Byte[] chart1 { get; set; }
public Byte[] chart2 { get; set; }
public Byte[] chart3 { get; set; }
public Byte[] chart4 { get; set; }
public Byte[] chart5 { get; set; }
public Byte[] chart6 { get; set; }

//This code is repeated for all 6 charts
public string getChart1
{
     get
     {
         string mimeType = "image/png";
         string base64 = Convert.ToBase64String(chart1);

         return string.Format("data: {0}; base64, {1}", mimeType, base64);
     }
}

查看:

   <table>
        for(int z = 0; z< Model.reportContentsCharts.Count / 2 ;z++) //At most 2 charts can be selected
        {                     
            <tr> 
                <td ="center">                             
                    <img src=@Model.reportContentsCharts[z]/>
                </td>

                <td ="center">
                    <img src=@Model.reportContentsCharts[z+1] />
                </td>                         
            </tr>                       
        }
   </table>

说谎问题: 目前,当我运行此代码时,它会返回一个损坏的图像。我在想这可能是一个语法问题?我有一些可以在我的网页上显示的图表。根据用户的输入,仅显示少数几个图形。我做的第一件事是在每个图形的html中硬编码一个位置,然后使用if()语句来确定是否显示图形。这样做的问题在于,根据用户输入,所选图形可以显示在单独的行上。这会产生错误的对齐和间距问题。

我知道这可能不是最好的方法,但我觉得这是最简单的解决方案。

感谢您的任何建议或帮助。

1 个答案:

答案 0 :(得分:2)

在我看来问题的根源是你设计糟糕的ViewModel。你需要规范它:

private Dictionary<string, byte[]> Charts = new Dictionary<string, byte[]>();

public string GetChart(string name)
{
     get
     {
         string mimeType = "image/png";
         string base64 = Convert.ToBase64String(Charts[name]);

         return string.Format("data: {0}; base64, {1}", mimeType, base64);
     }
}

public string AddChart(string name, byte[] data)
{
    Charts[name] = data;
}

然后你可以写这样的控制器:

IndexViewModel viewModel = new IndexViewModel()
{
      reportContentsCharts = reportContentsCharts
}
for (int i = 0; i < 6; i++)
{
    viewModel.AddChart("chart" + i, makeChart("chart" + i, DictionaryOfData));
}

最后,您可以这样写下您的观点:

<table>
        for (int z = 0; z < Model.reportContentsCharts.Count; z += 2)
        {                     
            <tr>
                for (int c = z; c < z + 2; c++)
                {
                    <td align="center">                             
                        if (c < Model.reportContentsCharts.Count)
                        {
                            <img src="@Model.GetChart(Model.reportContentsCharts[c])"/>
                        }
                    </td>
                }
            </tr>                       
        }
</table>