1

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

11
  • What you have looks fine although you should probably be passing the model and not just the list to the view... What is the problem you're having? Commented Aug 1, 2013 at 18:22
  • 1
    show the markup for the view... Commented Aug 1, 2013 at 18:23
  • I still do not have a complete markup I just created a table with headers, and I was trying to access PhoneDisplay List which I was not able. I am passing the entire model to the view now maybe that will fix my problem Commented Aug 1, 2013 at 18:29
  • When I pass the model I get an error, "Argument type 'AxlMVC.Models.HomePageModel' is not assignable to model type 'System.Collections.Generic.IEnumerable<AxmlMVC.Models.HomePateModel>"Sorry for the noob question this is my first MVC app Commented Aug 1, 2013 at 18:31
  • 3
    We would need to see the ops.getDisplayInfo(HpModel.ExtNum); method definition. Commented Aug 1, 2013 at 19:04

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Finally thanks, can you give me an example of how to use the annotations in the cshtml file. I have them annotated already in the model. Do I need to use DisplayForModel ??
@user2247823 you already have the @model AxlMVC.Models.HomePageModel declared so it's just @Html.DisplayNameFor(m => m.FirstName) which produces "First Name" taken from [Display(Name="First Name")]. And @Html.DisplayFor(m => m.FirstName) gives you the value of HpModel.FirstName.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.