locked
MVC 5 C# When calling a Html.Action from my controller it doesn't display anything. RRS feed

  • Question

  • User616798616 posted

    I am trying to display all the replies for a ticket on my details page.

    I'm calling my action with this line

    @Html.Action("_RepliesList", "Tickets")

    The ActionResult code is

            [ChildActionOnly]
            public ActionResult _RepliesList()
            {
                IEnumerable<Reply> replies;
    
                replies = db.Replies.ToList();
    
                return PartialView("_RepliesList", replies);
            }

    And my Partial view code is

    @model IEnumerable<Example.Reply>
    
    
    <h2>Replies</h2>
    
    
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Message)
            </th>
            <th></th>
        </tr>
    
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Message)
                </td>
    
            </tr>
        }
    
    </table>

    But when the page loads, where the replies should be its blank.

    The SQL table has data in it.

    No errors in compiling or loading the page.

    Any thoughts?

    Monday, August 12, 2019 1:15 PM

All replies

  • User-194957375 posted

    show the definition Example.Reply class.

    why 

    @Html.DisplayNameFor(model => model.Message)

    not in foreach ?

    if you put a breakpoint to

    @Html.DisplayFor(modelItem => item.Message)

     "item" contains data?

    Monday, August 12, 2019 1:40 PM
  • User616798616 posted

    Here is the Example.Reply.

    namespace Example
    {
        
        public partial class Reply
        {
            public int ReplyID { get; set; }
            public string Message { get; set; }
        }
    }

    This is the table header, just grabs the columns name.

    @Html.DisplayNameFor(model => model.Message)

    Hence why it doesn't need to be in foreach.

    Item should be the current index of the list for message.

    If I run this code on a separate page without using a partial view it works.

    Monday, August 12, 2019 1:46 PM
  • User-194957375 posted

    "item" contains data in debugger ?

    Or Model in 

    @foreach (var item in Model)
    Monday, August 12, 2019 1:58 PM
  • User616798616 posted

    It just randomly started working? I didn't changed anything except add a login check to another page.

    Monday, August 12, 2019 2:02 PM
  • User-194957375 posted

    I personally did not see any errors. Studio bug?

    Monday, August 12, 2019 2:06 PM
  • User616798616 posted

    I think so.

    Monday, August 12, 2019 2:17 PM
  • User2053451246 posted

    I don't think you found a bug.  ASP.Net will not directly serve a file that starts with an underscore; it has to be called from another view using Html.Partial, etc.  Did you by chance rename the file to not have an underscore in the name?

    Monday, August 12, 2019 8:00 PM
  • User1520731567 posted

    Hi Dmonro,

    I test your code and work fine in my project.

    I can't find anything wrong in your code.

    So I suggest you could add some breakpoints in your _RepliesList action,check if replies has value,

    and open F12 developer tool to check if there are other Javascript code that affect the results of the partial view.

    Best Regards.

    Yuki Tao

    Tuesday, August 13, 2019 8:39 AM