laitha0
laitha0

Reputation: 4336

Passing a List to View from Controller in .NET

I am just trying to pass a List and display it dynamically in a table in the View. I have a Homepage Model and Homepage controller and the variables are being set right, but I can't figure out how to pass it to the view.

My model looks like this:

 public class HomePageModel
 {
    [Display(Name = "First Name")]
    public string FirstName { get; set; }
    [Display(Name = "Last Name")]
    public string LastName { get; set; }
    [Display(Name = "ExtNum")]
    public string ExtNum { get; set; }
    [Display(Name = "PhoneDisplay")]
    public List<PhoneDisplay> PhoneDisplay { get; set; }
 }

and this is the controller:

 public ActionResult Homepage(HomePageModel HpModel)
    {
        ViewBag.Welcome = "Welcome: ";
        ViewBag.FirstName = HpModel.FirstName;   
        ViewBag.LastName = HpModel.LastName; 
        ViewBag.Extlbl = "Extension: ";
        ViewBag.Ext = HpModel.ExtNum;
        ViewBag.Todaylbl = "Today:";
        ViewBag.Today = DateTime.Now;
        DBOps ops = new DBOps();
        HpModel.PhoneDisplay = ops.getDisplayInfo(HpModel.ExtNum);
        return View(HpModel);
    }

PhoneDisplay is a list that contains a line index, a description string and a 4 digit number. Each user will have at least 1 item in this list and maximum 6. I was able to pass the other parameters and display them in the view but I can't find a way to pass the list and display that dynamically.

EDIT I made it this far but still can't find the list items.

    @model AxlMVC.Models.HomePageModel
    <table>
    <caption style="font-weight:bold">Your Phone Information</caption>    
    <tr>
        <th>Line Index</th>
        <th>Display</th>
        <th>Extension Number</th>
    </tr>
    @{
    foreach (var item in Model.PhoneDisplay) //problems here
    {
     <tr>
        <td>
             @Html.Display(item.numplanindex)
        </td>
        <td>
             @Html.Display(item.display)
        </td>
        <td>
             @Html.Display(item.dnorpattern)
        </td>
     </tr>
    }
 }
    </table>

EDIT I debugged the cshtml file and the items in the foreach loop are being passed just fine too, but the table is not showing on the page and neither are the items all I can see is the caption and the headers for each column

enter image description here

Upvotes: 1

Views: 1597

Answers (1)

Patko
Patko

Reputation: 4423

Html.Display displays "data from the ViewData dictionary or from a model" as stated on MSDN. What it means is that it searches for the key in the ViewData dictionary with the value you pass in or a property in the Model with the specified name. E.g. Display("test") would search ViewData for the "test" key and the Model for the property named test. Since you are passing in property values that cannot work. Your options are:

  • Output the value directly, @item.numplanindex. This will output a string representation of the value.
  • Use Display, although this is not recommended. You could do Display("PhoneDisplay[1].numplanindex") to display numplanindex property of the second item in list.
  • Use DisplayFor, like DisplayFor(model => item.numplanindex). This is a strongly typed version of Display. It will either displays a string representation of the value or a template for the type, if you have one. You can also manage how the output is displayed via Data Annotations, e.g. DisplayFormatAttribute.
  • Use DisplayTextFor, like DisplayTextFor(model => item.numplanindex). This method outputs the string representation of the value.

Since you already have data annotations on the model, you could modify your view like this: @model AxlMVC.Models.HomePageModel

<table>
  <caption class="tableCaption">Your Phone Information</caption>
  <tr>
    <th>@Html.DisplayNameFor(model => model.PhoneDisplay[0].numplanindex)</th>
    <th>@Html.DisplayNameFor(model => model.PhoneDisplay[0].display)</th>
    <th>@Html.DisplayNameFor(model => model.PhoneDisplay[0].dnorpattern)</th>
  </tr>
  @{ 
  foreach (var item in Model.PhoneDisplay)
  {
    <tr>
      <td>@Html.DisplayTextFor(model => item.numplanindex)</td>
      <td>@Html.DisplayTextFor(model => item.display)</td>
      <td>@Html.DisplayTextFor(model => item.dnorpattern)</td>
    </tr>
  }
  }
</table>

The line @Html.DisplayNameFor(model => model.PhoneDisplay[0].numplanindex) also works if PhoneDisplay contains no items. Only property metadata is collected, expression as such is not executed.

Upvotes: 1

Related Questions