I have the following ViewModel
public class ShowQuestionsViewModel
{
public String CategoryName { get; set; }
public String CategoryNumber { get; set; }
public List<QuestionViewModel> QuestionsInCategory { get; set; }
public ShowQuestionsViewModel()
{
QuestionsInCategory = new List<QuestionViewModel>();
}
}
In my controller i set the properties like this (snippet):
ShowQuestionsViewModel sqvmsub = new ShowQuestionsViewModel();
var questionsSub = db.Question.Where(y => y.category_id == sub.category_id).ToList();
if (questionsSub != null)
{
sqvmsub.CategoryName = sub.category_name;
sqvm.CategoryNumber = sub.prefix;
.....
}
When i try to display the Properties in my View, the CategoryNumber property is empty, even though it is set in controller (i checked by debugging)..:
@foreach (var item in Model)
{
if (item.QuestionsInCategory.Count != 0)
{
<h3>@Html.LabelFor(y => item.CategoryName, item.CategoryName)</h3>
<h3>@Html.LabelFor(y => item.CategoryNumber, item.CategoryNumber)</h3>
@Html.LabelFor(y => item.CategoryNumber, item.CategoryNumber)
Only displays the text “CategoryNumber”
If i try to change my controller like this:
sqvmsub.CategoryName = sub.prefix;
It displays what it is supposed to, which means there is nothing wrong with sub.prefix, but with CategoryNumber in the ShowQuestionsViewModel..
Does anyone have an idea of what is wrong?
I was apparently not setting the properties correctly. I changed it to:
It now work. Thanks for the help.