문자열을 변수로 변환하여 어떻게 변수로 사용할 수 있습니까? 루프를 통해 내 모델에서 데이터를 호출 할 수있는 문자열 목록이 있습니다.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() 문을 사용하여 그래프를 표시할지 여부를 결정하는 것입니다. 이 문제는 사용자 입력에 따라 선택한 그래프가 별도의 줄에 나타날 수 있다는 것입니다. 이로 인해 잘못된 정렬 및 간격 문제가 발생합니다.
이 방법이 최선의 방법은 아닐 수도 있지만 가장 간단한 해결책이라고 생각했습니다.
제안이나 도움에 감사드립니다.
사용 foreach 문을 고리? 나는 당신의 문제를 이해하지 못합니다. 이 코드를 수정하려는 코드 섹션에 붙여 넣을 수 있습니까? –
주요 문제는보기에 있습니다. 일반적으로 난 (잘 작동) 할 것이지만 대신 reportContentsCharts에 저장된이 이름을 참조하려고합니다. – Sunday1290
왜 뷰 모델 내에 차트 목록이 없습니까? 'public List 도표 {get; set;}'; –
scheien