2

I've researched this issue and can not find a solution. My problem is that in my view, I can not view the properties of my ViewModel (e.g., Model.Supplier.SupplerName). Intellisense will only allow me to go Model.Suppliers, or Model.ToolTypes, or Model.ToolTypesSuppliers . I can not get the properties of each of these models that I included into the ViewModel.

ViewModel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ToolOrder.Models
{
    public class ToolTypeSupplierViewModel
    {
        public ToolTypeSupplierViewModel(IEnumerable<ToolType> tooltypes, IEnumerable<Supplier> suppliers, IEnumerable<ToolTypeSupplier> tooltypesuppliers)
        {
            this.ToolTypes = tooltypes;
            this.Suppliers = suppliers;
            this.ToolTypeSuppliers = tooltypesuppliers;
        }

        public IEnumerable<ToolType> ToolTypes { get; private set; }
        public IEnumerable<Supplier> Suppliers { get; private set; }
        public IEnumerable<ToolTypeSupplier> ToolTypeSuppliers { get; private set; }

    }
}

Controller:

public ActionResult ListToolTypeSupplierOptions()
{
    var _tooltypelist =_repository.FetchAllToolTypeOptions();
    var _supplierlist = _repository.FetchAllSupplierOptions();
    var _toolstypesupplierlist = _repository.FetchAllToolTypeSupplierOptions();

    return View(new ToolTypeSupplierViewModel(_tooltypelist, _supplierlist, _toolstypesupplierlist));
}

View:

@model IEnumerable<ToolOrder.Models.ToolTypeSupplierViewModel>


<table>
    <tr>
        <th>Supplier</th>
        <th>Tool TYpe</th>
        <th>Created</th>
        <th>Last Modified </th>
    </tr>

@foreach (var item in Model.) {
    <tr>
        <td>
        </td>
    </tr>
}

</table>

Any help would be appreciated or if any further clarification is required, I can follow up. I apologize if this is somewhat vague, I've been up trying to figure out why I can not pull the the properties of each Model that I've included in my ViewModel. Thank you in advance.

2
  • 1
    Are you trying to see the Intellisense properties in the foreach statement off of the Model or inside the loop off of the item? In the statement of the foreach you are only going to get Intellisense for collection types. Then inside the loop if you did item. then you should see the properties for which ever collection type you chose. Commented Apr 8, 2011 at 5:33
  • @munozca a total other topic but a tip: I would use a Facade Layer which is calling your 3 repository methods. Then your facade layer would return a data transfer object to the rich client :) Commented Feb 3, 2013 at 14:12

3 Answers 3

4

Try this:

@foreach (var item in Model) {
  @foreach (var supplier in item.Suppliers){
    <tr>
        <td>
           @supplier.SupplierName
        </td>
    </tr>
  }
}

It seems you need to loop through the Suppliers it's IEnumerable.

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

2 Comments

That worked perfectly. Thank you! It is strange that in this article LINK Stephen Walker was able to chain down the ViewModel in his view. Any reason why I wasn't able to?
@munozca In the last example, he is just using Single object as the ViewModel (ProductViewModel) while you are using IEnumerable<ToolOrder.Models.ToolTypeSupplierViewModel> object. You can do the same if you use ToolOrder.Models.ToolTypeSupplierViewModel Model.
1

Your question is somehow misleading, so i'll try to answer both cases.

  1. As i see you pass single instance of ToolTypeSupplierViewModel to your view from controller, and not enumerable. If this is not mistake, than mistake is in your view - model should be not IEnumerable of ToolTypeSupplierViewModel, but single ToolTypeSupplierViewModel. In this case, you could iterate over Model.ToolTypes, Suppliers, etc. Directly
  2. If the mistake is not in view, but in controller and model should be of type IEnumerable<ToolTypeSupplierViewModel>, than first you sould iterate over Model directly, as it IS Enumerable itself. Its items are ToolTypeSupplierViewModel. In this case, code will be as ysrb suggested

Comments

0

Try @using ToolOrder.Models at the top of your Razor file.

or in web.config, find the namespaces section and add it there

  <namespaces>
    ...
    <add namespace="ToolOrder.Models" />

Comments

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.