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:
- If the structure of the Model View and controller is incorrect
- 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.