-1

I have the following razor code:

 <div class="panel panel-default col-xs-10 col-sm-10 col-md-10 col-lg-10">
  <div class="panel-body">
    @if (Model != null)
    {
      var m = Model.Requests.FileData.OrderBy(p => p.FileName);
      if (m.Count() > 0)
      {
        foreach (var item in m)
        {
          if (Model.data.ToLower().Contains("dns"))
          {
            <div class="row">
              <span>
                @Html.DisplayFor(mi => item.FileName, new { @class = "form-label" })
                <span class="glyphicon glyphicon-trash"></span>
              </span>
            </div>
          }
        }
      }
    }
  </div>
</div>

This is a panel that shows uploaded files that are in the database. After selecting a file, the controller puts it in the database and then returns the partialview that has the above code.

When I debug the application and step through the above code, it does what it should do, but when I look at the html of the rendered page, the panel is empty.

Rendered page:

<div class="panel panel-default col-xs-10 col-sm-10 col-md-10 col-lg-10">
  <div class="panel-body">
  </div>
</div>

What am I missing here, why is the html not rendered?

[EDIT]

Break point:

enter image description here

I figured out that only everything in the second IF section is not rendering elements.

Using this:

<span class="form-label">@item.FileName</span>

Doesn't render anything either.

[EDIT]

Changed the viewmodel to have a property called List FileData and loaded the data in to it.

Then changed the Razor to read from it.

<div class="panel panel-default col-xs-10 col-sm-10 col-md-10 col-lg-10">
  <div class="panel-body">
    @if (Model != null && Model.FileData != null && Model.FileData.Any())
    {
      var m = Model.FileData.OrderBy(p => p.FileName);
      foreach (var item in m.ToList())
      {
        <div class="row">
          <span>
            <span class="form-label">@item.FileName</span>
            <span class="glyphicon glyphicon-trash"></span>
          </span>
        </div>
      }
    }
  </div>
</div>

enter image description here

Unfortunately, still no rendering.

Eric
  • 339
  • 3
  • 14
  • 4
    This would suggest that `Model.Requests.FileData.OrderBy(p => p.FileName)` yields an empty collection or that `Model.data.ToLower().Contains("dns")` is `false`. Assume you've ensured that neither of these is happening? – spender Feb 03 '16 at 15:24
  • 1
    Can you confirm that a breakpoint inside the second "if" statement is hit when debugging? – rashleighp Feb 03 '16 at 15:26
  • no, it is not empty. I checked, even if I upload 5 files, to goes through all 5 in the foreach loop. – Eric Feb 03 '16 at 15:26
  • Yes, break points on each "if" is hit. – Eric Feb 03 '16 at 15:28
  • 1
    Why are you using a DisplayFor? Set the class on the span and use item.FileName inside the span...Also a good coding standard would be to use if(m.Any()) instead of (m.Count() > 0). – Grendizer Feb 03 '16 at 15:31
  • 1
    I agree the comment that @spender made. If you were actually going through the loop we'd at least see the static HTML (e.g. ` 0` check. – Craig W. Feb 03 '16 at 15:32
  • 1
    Is `ata.ToLower().Contains("dns")` true or false ? – Shyju Feb 03 '16 at 15:33
  • I have updated the question with an image of the breakpoint. To answer the comments: I have tried it with just span too, same result. As seen in the image, the data is there. – Eric Feb 03 '16 at 15:35
  • 1
    Could you please submit the action method too? – Sam FarajpourGhamari Feb 03 '16 at 15:42
  • 1
    I would as I mentioned not use the DisployFor Html extension but instead add the item.filename inside the span. http://stackoverflow.com/questions/9465376/when-should-i-use-html-displayfor-in-mvc – Grendizer Feb 03 '16 at 15:59
  • Updated the question. Also tried with SPAN, no change. Changed m.Count() into m.Any(), still no change. – Eric Feb 03 '16 at 16:03

2 Answers2

1

In the action method try to include relative objects in your query also. Consider this example:

var model = db.MyModels
    .Include(m=>m.Requests.FileData)
    .FirstOrDefault(m=>m.ID==Id);

Also don't forget to add System.Data.Entity namespace.

Sam FarajpourGhamari
  • 14,601
  • 4
  • 52
  • 56
  • I'm not sure what you mean. Stepping thought the razor with a break point shows the data is there. See the image. – Eric Feb 03 '16 at 16:22
  • 2
    I know. This is happening because of EF's lazy loading behavior. When you attach a debugger proxy objects are filed while you are watching them. – Sam FarajpourGhamari Feb 03 '16 at 16:29
  • I created a new property in my ViewModel and put the FileData list in it. Then I changed the razor to use this property. But still no rendering. – Eric Feb 03 '16 at 16:31
  • 1
    How you put the `FileData` list in new property? – Sam FarajpourGhamari Feb 03 '16 at 16:36
  • I have added a second image, showing the data is in the FileData list. – Eric Feb 03 '16 at 16:41
  • 1
    As I said before when you watch the object items are filed. You must include items in your query. Moving to an other property without including doesn't help. – Sam FarajpourGhamari Feb 03 '16 at 16:49
  • I just found out problem. The file upload is done via Ajax and then the controller returns the refreshed viewmodel to Ajax. In the success part of Ajax it should update the view, but the data is returned in the error part. There is no error in the data, so I have now to figure out why it is returning as an error. – Eric Feb 03 '16 at 16:52
1

Well I found the problem. In my Jquery Ajax code I had set the dataType to 'json'. Because of that Ajax was expecting json in return but got HTML and then it was an error so my panel wasn't updated. So the statusText of the returned data object was 'OK' and there was no error.

I found out what I did wrong here

I removed the dataType option and now everything is working properly.

Thanks for all the attempts to help.

Community
  • 1
  • 1
Eric
  • 339
  • 3
  • 14
  • Where in your question is there any mention of using jquery and ajax! –  Feb 03 '16 at 21:18
  • I didn't mention it before because the debugger put me off track as it was showing the data in the view so I didn't make the link that the problem could be there. – Eric Feb 04 '16 at 22:08
  • You should have deleted the question - this is of no use to anyone. –  Feb 04 '16 at 22:10