很抱歉,如果措辞不好,但我很困惑,已经很晚了,但是我会尽力解释。因此,我尝试不使用实体框架将数据库中的所有订单显示到表中。问题是,当我转到orders / getAllOrders时,我得到一个大错误,说“视图”,然后是一堆
<tr><td>2</td><td>Javier</td><td>1</td><td>Groovestring</td><td>3.950000</td></tr>etc..
其中包含我的数据,但我不知道如何正确显示它。我知道我的查询工作正常,并且可以很好地检索我的数据,但是在显示它时我很困惑
这是我目前为控制器提供的。
namespace Website.Controllers
{
public class OrderController : Controller
{
private static bool CanConnectToDb()
{
var cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
try
{
cnn.Open();
return true;
}
catch (Exception exception)
{
return false;
}
finally
{
if (cnn.State != ConnectionState.Closed)
cnn.Close();
}
}
public ActionResult getAllOrders()
{
// Connect to Database
var cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
cnn.Open();
// Create Command
SqlCommand cmd = new SqlCommand(
@"SELECT CustomerOrderId, FirstName ,ProductId, [Name], Price
FROM OrderProduct AS o
INNER JOIN CustomerOrder co ON co.id = o.CustomerOrderId
INNER JOIN Product p ON p.Id = o.ProductId
INNER JOIN Customer c ON c.Id = co.CustomerId
ORDER BY ProductId"
);
cmd.Connection = cnn;
string store = "";
// Read from Database
SqlDataReader reader = cmd.ExecuteReader();
var orders = new OrderIndexViewModel();
while (reader.Read())
{
orders.CustomerOrderId = reader.GetInt32(0);
orders.FirstName = reader.GetString(1);
orders.ProductId = reader.GetInt32(2);
orders.Name = reader.GetString(3);
orders.Price = reader.GetDecimal(4);
store += "<tr><td>" + orders.CustomerOrderId + "</td><td>" + orders.FirstName + "</td><td>" + orders.ProductId + "</td><td>" + orders.Name + "</td><td>" + orders.Price + "</td></tr>";
}
cnn.Close();
return View(store);
}
}
}
编辑:当我将返回值更改为
rerturn View(orders);
我收到此错误
The model item passed into the dictionary is of type 'Website.Models.OrderIndexViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Website.Models.OrderIndexViewModel]'.
这是我的模特
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Website.Models
{
public class OrderIndexViewModel
{
public int CustomerOrderId { get; set; }
public string FirstName { get; set; }
public int ProductId { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
}
最后这就是我的观点
@model IEnumerable<Website.Models.OrderIndexViewModel>
@{
ViewBag.Title = "getAllOrders";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>getAllOrders</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
Customer Oder Id
</th>
<th>
First Name
</th>
<th>
ProductId
</th>
<th>
Name
</th>
<th>
Price
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CustomerOrderId)
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.ProductId)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</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>
任何帮助都会得到感激
答案 0 :(得分:0)
您已将字符串发送到模型,但必须将集合发送到模型,然后在表中显示
答案 1 :(得分:0)
您的视图期望使用IEnumerable<Website.Models.OrderIndexViewModel>
作为模型,但是您要向其传递Website.Models.OrderIndexViewModel
的单个实例。
这可能是代码中的另一个错误,因为这只会渲染select语句中的最后一项。因此,您需要这样的东西,它将为每个结果渲染一行:
var orders = new List<OrderIndexViewModel>();
while (reader.Read())
{
var order = new OrderIndexViewModel();
order.CustomerOrderId = reader.GetInt32(0);
order.FirstName = reader.GetString(1);
order.ProductId = reader.GetInt32(2);
order.Name = reader.GetString(3);
order.Price = reader.GetDecimal(4);
orders.Add(order);
}
cnn.Close();
return View(orders);