0

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:

a busy cat

Is there another way to do this?

Community
  • 1
  • 1
Evilduky
  • 131
  • 1
  • 2
  • 8

2 Answers2

0

If there are items inside the list and the CriteriaType and TextToSearch properties are not null or empty then this should work. Also make sure that there's no some CSS rule that is hiding the div. You could inspect your DOM with FireBug or Chrome Developer Toolbar and analyze all CSS rules applied to the generated div.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 'CriteriaType' and 'TextToSearch' have values. All HTML code inside the foreach loop isn't display, I don't understand why. Outside the loop, HTML code is display correctly with style. – Evilduky Sep 19 '12 at 15:42
  • Then this probably means that `Model.CriteriaListToSearch` is empty. – Darin Dimitrov Sep 19 '12 at 15:43
  • I put a break point in my view, the list isn't empty, I have one criteria with 2 values – Evilduky Sep 19 '12 at 15:45
  • @Evilduky You need to path model data to view from action in controller 'return View(model)' – webdeveloper Sep 19 '12 at 15:46
  • yes take a look to my controller code: `[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); }` – Evilduky Sep 19 '12 at 15:51
  • @Evilduky, what if the `model` parameter is not null in your controller action? Have you verified that in this case the `CriteriaListToSearch` property contains something? Because you are only adding one element if model is null. – Darin Dimitrov Sep 19 '12 at 15:53
  • If it's not null, I want to add criterias to others existing. Otherwise there is only one element in the list. – Evilduky Sep 19 '12 at 15:54
  • You seem to be binding `crit.CriteriaType` and `crit.TextToSearch` from the corresponding properties in the `model`. If the `model` is null, you are instantiating it: `model = new Search();` and of course you cannot expect those 2 properties to contain any values. They will simply be empty. That's why you get empty results in your view. – Darin Dimitrov Sep 19 '12 at 15:57
  • I had a screenshot to my post, check it! – Evilduky Sep 19 '12 at 16:09
  • @webdeveloper I did it, there are my values in – Evilduky Sep 19 '12 at 16:10
  • So how does your generated HTML markup look like? Can you see the generated div? – Darin Dimitrov Sep 19 '12 at 16:11
  • @Evilduky I am sure that something must be generated, maybe some jquery magic remove content. Please, make screenshot from browser source. – webdeveloper Sep 19 '12 at 16:15
  • @DarinDimitrov my HTML code generated:
    Filter
    As you see, my div
    is missing.
    – Evilduky Sep 20 '12 at 07:33
  • @webdeveloper when I remove the foreach loop, it appears normaly. – Evilduky Sep 20 '12 at 07:35
  • @Evilduky You can try add '.ToList()' for test and try simple loop with `for`, then post results here. – webdeveloper Sep 20 '12 at 07:43
  • the same behavior with for loop `
    @for (int i = 0; i < Model.CriteriaListToSearch.Count; i++) {
    @Model.CriteriaListToSearch[i].TextToSearch
    }
    Filter
    `
    – Evilduky Sep 20 '12 at 08:04
0

What does your model look like? Razor may be calling ToString() in order to display your data.

Charles Burns
  • 10,310
  • 7
  • 64
  • 81
  • Meant to ask about Criteria, but all I had to do was scroll down. I haven't spotted anything that looks wrong. Tried a few things. You've already stepped through. With no other info, at this point what I would do is check for proper includes, library versions, reboot windows, and other not-very-useful generic advice. I notice the loop uses `"Namespace.Models.Criteria" and `"OneSelfSite.Models.Criteria" elsewhere. "Namespace" is probably just for the SO question, but can you replace that with "@foreach (var crit in Model..." to test for the unlikely event of lossy casting? – Charles Burns Sep 20 '12 at 14:57
  • thanks a lot for your investigation! I don't understand what is wrong with my code but I tried to make it differently by using Ajax.beginForm instead of Html.BeginForm in order to fill my div `
    value
    ` dynamicaly like here [http://stackoverflow.com/a/5410121/1130080](http://stackoverflow.com/a/5410121/1130080) and it works!
    – Evilduky Sep 20 '12 at 15:03