2014-09-09 2 views
1

문자열을 변수로 변환하여 어떻게 변수로 사용할 수 있습니까? 루프를 통해 내 모델에서 데이터를 호출 할 수있는 문자열 목록이 있습니다.C# MVC - 문자열을 변수로 변환

내 코드 :

컨트롤러 :

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 [email protected][z]/> 
       </td> 

       <td ="center"> 
        <img [email protected][z+1] /> 
       </td>       
      </tr>      
     } 
    </table> 

: 은 현재 내가 실행 이 코드는 나에게 깨진 이미지를 돌려 준다. 이것이 구문 문제 일 수 있다고 생각합니까? 내 웹 페이지에 표시 할 수있는 소수의 그래프가 있습니다. 사용자의 입력에 따라 그래프 중 일부만 표시됩니다. 내가 한 첫 번째 일은 각 그래프에 대해 HTML에서 위치를 하드 코딩 한 다음 if() 문을 사용하여 그래프를 표시할지 여부를 결정하는 것입니다. 이 문제는 사용자 입력에 따라 선택한 그래프가 별도의 줄에 나타날 수 있다는 것입니다. 이로 인해 잘못된 정렬 및 간격 문제가 발생합니다.

이 방법이 최선의 방법은 아닐 수도 있지만 가장 간단한 해결책이라고 생각했습니다.

제안이나 도움에 감사드립니다.

+0

사용 foreach 문을 고리? 나는 당신의 문제를 이해하지 못합니다. 이 코드를 수정하려는 코드 섹션에 붙여 넣을 수 있습니까? –

+0

주요 문제는보기에 있습니다. 일반적으로 난 (잘 작동) 할 것이지만 대신 reportContentsCharts에 저장된이 이름을 참조하려고합니다. – Sunday1290

+1

왜 뷰 모델 내에 차트 목록이 없습니까? 'public List 도표 {get; set;}'; – scheien

답변

2

문제의 근원이 저조한 디자인의 ViewModel 인 것처럼 보입니다. 당신은 그것을 정상화해야

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

을 그리고 마지막으로,이 같은보기 쓸 수 있습니다 :

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; 
} 

그런 다음이 같은 컨트롤러 뭔가를 쓸 수

<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> 
+0

Brilliant. 내가 변경해야만했던 몇 가지 사항. ViewModel에서 메서드 및 속성을 작동하지 않는 것으로 결합하기 때문에 Get {}을 삭제해야했습니다. AddChart 메서드에서 값을 반환하지 않았기 때문에 오류가 발생했습니다. 이것은 기술적으로 정확하지 않을 수도 있지만 그 이름을 반환했습니다. 이러한 변경 사항이 모두 마 법적으로 효과가있었습니다! – Sunday1290