I was using linq but I ran into something that I need to write fast so I am trying to get raw sql to work. This is my first project in mvc so I am a newbie but here is my controller code
public ActionResult Index(string Vendors)
{
if (Vendors != "")
{
string query = "SELECT V.VENDOR_NAME, SUM(POL.ORDER_QTY * POL.UNIT_PRICE) AS AMOUNT_SPENT, MONTH(PO.ORDER_DATE) AS MO, YEAR(PO.ORDER_DATE) AS YR, M.ITEM_TYPE AS ITEM_TYPE "
+ "FROM PO_LINE POL"
+ "JOIN PURCH_ORD PO ON PO.PO_NUM = POL.PO_NUM"
+ "JOIN VENDOR V ON V.VENDOR_ID = PO.VENDOR_ID"
+ "JOIN MATERIAL M ON M.ITEM_ID = POL.ITEM_ID"
+ "WHERE V.VENDOR_NAME =" + Vendors
+ "GROUP BY V.VENDOR_NAME, M.ITEM_TYPE, YEAR(PO.ORDER_DATE), MONTH(PO.ORDER_DATE)"
+ "ORDER BY V.VENDOR_NAME";
var data = db.Database.SqlQuery<Reports>(query);
ViewBag.Vendors = new SelectList(db.Vendors, "VENDOR_NAME", "VENDOR_NAME");
return View(query.ToList());
}
this is my modal code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GreenfieldGroup2.Models.Reports
{
public class Reports
{
public string VENDOR_NAME { get; set; }
public decimal AMOUNT_SPENT { get; set; }
public string ITEM_TYPE { get; set; }
public DateTime ORDER_DATE { get; set; }
public string ITEM_NAME { get; set; }
public int STOCK_QTY { get; set; }
public string WARD_NAME { get; set; }
public string WARD_LOCATION { get; set; }
public int ITEM_ID { get; set; }
public DateTime DEL_DATE { get; set; }
}
}
The model item passed into the dictionary is of type 'System.Collections.Generic.List1[System.Char]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[GreenfieldGroup2.Models.Reports.Reports]'.
is my error I am not sure what this means. Below is my view Code
@model IEnumerable<GreenfieldGroup2.Models.Reports.Reports>
@{
ViewBag.Title = "Index";
}
<br />
<head>
<style>
body {
padding-top: 100px
}
</style>
</head>
<body>
@using (Html.BeginForm("Index", "PurchasingReport", FormMethod.Get))
{
<table>
<tr>
<td align="left"><text>Select a Vendor:</text>
</td>
<td align="left">@Html.DropDownList("Vendors", "All Vendords")</td>
</tr>
<tr>
<td align="Left">
<input type="submit" value="submit" onsubmit="return Verify()"/>
</td>
</tr>
</table>
}
<table border="1">
<tr>
<th>
@Html.DisplayNameFor(model => model.VENDOR_NAME)
</th>
<th>
@Html.DisplayNameFor(model => model.AMOUNT_SPENT)
</th>
<th>
@Html.DisplayNameFor(model => model.ITEM_TYPE)
</th>
<th>
@Html.DisplayNameFor(model => model.ORDER_DATE)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.VENDOR_NAME)
</td>
<td>
@Html.DisplayFor(modelItem => item.AMOUNT_SPENT)
</td>
<td>
@Html.DisplayFor(modelItem => item.ITEM_TYPE)
</td>
<td>
@Html.DisplayFor(modelItem => item.ORDER_DATE)
</td>
</tr>
}
</table>
</body>
any help with would be gratefully appreciated this is for a school project
You should change your return statement
return View(query.ToList());
to the following:
return View(data);
As I understand from the code you have posted, below
var data = db.Database.SqlQuery<Reports>(query);
you assign a reference to a sequence of Reports
objects to the data
. In other words a the type of data
is IEnumerable<GreenfieldGroup2.Models.Reports.Reports>
as I can infer from your code.
So that is needed to be passed to the View and not the sql statement, which is just a string.