1

I normally work with SQL and SSRS and am new to C# and working on my first MVC project.

My objective was to create page displaying a table with parent and their associated child records - without the parents repeating for each child value.

Column1 Primary Key from parent table

Column 2 Name from parent table

Column 3 List of child names

To accomplish this I attempted to create a Model View.

I believe I have made a number of mistakes related to placement of code

Model View

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace SupplierItemTest.Models
{
    public class VM_Trade
    {

        [Required]
        [Display(Name = "Tax Info")]
        public virtual string TaxpayerID { get; set; }

        [Required]
        [Display(Name = "Entity Name")]
        public virtual string Supplier { get; set; }

        [Required]
        [Display(Name = "Trading Names")]
        public virtual string SupplierTradingName1 { get; set; }

        [Required]
        [Display(Name = "Supplier Number")]
        public virtual string EhSupplierNumber { get; set; }
    }
}

Controller View

    using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SupplierItemTest.Models;


namespace SupplierItemTest.Controllers
{
    public class TradingNameController : Controller
    {
        private SupplierItemEntities db = new SupplierItemEntities();

        //
        // GET: /TradingName/

        public ActionResult Index()
        {
            var Trade_View =
                from s in db.Suppliers
                join st in db.SupplierTradingNames on s.TaxpayerID equals st.TaxpayerID
                join sn in db.EhSuppliers on s.TaxpayerID equals sn.TaxpayerID
                orderby s.TaxpayerID
                select new VM_Trade
                 {
                     SupplierTradingName1 = st.SupplierTradingName1,
                     TaxpayerID = s.TaxpayerID,
                     Supplier = s.SupplierName,
                     EhSupplierNumber = sn.EhSupplierNumber
                      };                     
            return View(Trade_View.ToList());

View

@model IEnumerable<SupplierItemTest.Models.VM_Trade>
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
           @Html.DisplayNameFor(model => model.TaxpayerID)
        </th>
        <th>
           @Html.DisplayNameFor(model => model.Supplier)
        </th>
        <th>
          @Html.DisplayNameFor(model => model.EhSupplierNumber)
        </th>
        <th>
          @Html.DisplayNameFor(model => model.SupplierTradingName1)
        </th>
        <th></th>
    </tr> 

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.TaxpayerID)
       </td>
        <td>
             @Html.DisplayFor(modelItem => item.Supplier)
        </td>
         <td>
           @Html.DisplayFor(modelItem => item.EhSupplierNumber)
        </td>
        <td>
           @Html.DisplayFor(modelItem => item.SupplierTradingName1)
        </td>

        <td>
            @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
    </tr>
}

</table>
  • Initially I tried to place IEnumerable in the View Model my controller LINQ returned the following

Error "Cannot implicitly convert type 'string' to 'Systen.Collections.Generic.IEnumerable

I was hoping some could tell me:

  1. If the structure of the Model View and controller is incorrect
  2. How to return a single value for the parent columns

From what I have read I have the following very wrong:

  • IEnumerable should be used in the Model View
  • LINQ should not be using joins as their is a relationship in the database
  • The view should not contain IEnumerable
  • I have no idea how to return single values from a parent column and multiple values for child rows.

I have been reading for a couple of day and just can seem to get anything to work.

tereško
  • 58,060
  • 25
  • 98
  • 150
Soap
  • 13
  • 3

3 Answers3

0

In your View you fist access your model like it be an object then few lines below you are trying to enumerate it.

So I suggest you following

  1. Insert breakpoint in your View and check what Model is
  2. If this not help use Ctrl+Alt+E and mark to stop Visual Studio on every exception
Piotr Stapp
  • 19,392
  • 11
  • 68
  • 116
0

add () after select new VM_Trade as you are upto creating a new object of VM_Trade class.

0

There are problems with your view model and view. You can reference the basic structure for a similar situation here.

I would also suggest you to have a look at Music Store application tutorial which explain these concept very well.

You are on the right path, just few changes in the ViewModel and View, and you will be done. Once the structure is fixed, you will have more specific question that the community will be happy to answer.

EDIT: Just a bit of help, there is this Nuget package "EntityFramework.Sample" which give you sample models and a demo DbContext set to play around.

Community
  • 1
  • 1
SBirthare
  • 5,117
  • 4
  • 34
  • 59