I want to display content of a list that is a property of my model but HTML code rendered is empty, here my code:
View
@foreach (Namespace.Models.Criteria crit in Model.CriteriaListToSearch)
{
<div class="selectedItem">@crit.CriteriaType : @crit.TextToSearch</div>
}
View Model
public class Search
{
public IEnumerable<EquipmentModel> Equipments;
public IEnumerable<SparePartsModel> SpareParts;
public List<Criteria> CriteriaListToSearch;
public Search()
{
CriteriaListToSearch = new List<Criteria>();
}
#region Criteria
private List<SelectListItem> _CriteriaList;
public List<SelectListItem> CriteriaList
{
get
{
if (_CriteriaList == null)
{
_CriteriaList = new List<SelectListItem>(4)
{
new SelectListItem { Value = "", Text = "Select an option"},
new SelectListItem { Value = "1", Text = "Name/Number"},
new SelectListItem { Value = "2", Text = "Type"},
new SelectListItem { Value = "3", Text = "Description"}
};
}
return _CriteriaList;
}
}
[Display(Name = "Options")]
[Required(ErrorMessage = "Please select an option.")]
public string CriteriaSelected { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "The search field is required.")]
[Display(Name = "Search")]
[StringLength(100)]
public string TextToSearch { get; set; }
#endregion
}
public class Criteria
{
public string CriteriaType;
public string TextToSearch;
}
Model.CriteriaListToSearch is my list, there is one item, I checked with break point. I try to display it by prefixing with tag, failed.
[HttpPost]
public ActionResult AddCriteria(Search model)
{
if (model == null) model = new Search();
Criteria crit = new Criteria();
crit.CriteriaType = model.CriteriaSelected;
crit.TextToSearch = model.TextToSearch;
model.CriteriaListToSearch.Add(crit);
return View("Index", model);
}
Debug:
Is there another way to do this?