-1
在我的MVC控制器中,我創建了一個「對象」,我簡化爲「學生」。我使用ViewBag將其傳遞給我的視圖。如何從HTML中訪問C#中創建的對象的值?
在我的控制,我有:
ViewBag.Student = new {
Id = "52",
Value = JsonConvert.SerializeObject(new
{
StudentName = oneStudent.Name,
Age = oneStudent.Age,
FavoriteColor = oneStudent.Color,
Height = oneStudent.Height
}),
Text = oneStudent.Name + " --- " + oneStudent.Age,
};
我開始了我的調用ViewBag,使用
@{
var student = ViewBag.Student;
}
,我想用量程(或任何類似)來顯示數據。我試圖在html代碼中使用「@ Student.Value」中的一些代碼,但那不起作用。
任何幫助將不勝感激。謝謝。
ViewBag是動態的,那麼 可以使用反射或更改您的代碼到 動態學生= ViewBag.Student; –
在視圖中使用視圖模型比viewbag更容易。 –
@AhmedGalal感謝您的建議,我會研究它。 – JoeFromAccounting