谁能想到为什么当我单击详细信息链接时我没有得到所需的输出?我得到错误:The model item passed into the dictionary is of type 'System.Collections.Generic.List1[AW_Internet.Models.ProdList]', but this dictionary requires a model item of type 'AW_Internet.Models.ProdList'.
控制器:
public ActionResult Details(int id1 = 0, int id2 = 0)
{
string test = "SELECT SalesLT.Product.ProductID, SalesLT.ProductDescription.ProductDescriptionID, SalesLT.Product.Name, SalesLT.Product.ListPrice, LEFT(SalesLT.ProductDescription.Description, 20) as Descrption FROM SalesLT.Product, SalesLT.ProductDescription, SalesLT.ProductModelProductDescription WHERE SalesLT.Product.ProductModelID = SalesLT.ProductModelProductDescription.ProductModelID AND SalesLT.ProductModelProductDescription.ProductDescriptionID = SalesLT.ProductDescription.ProductDescriptionID AND SalesLT.Product.ProductID = " + id1 + " AND SalesLT.ProductDescription.ProductDescriptionID = " + id2;
var viewModel = db.Database.SqlQuery<ProdList>(test).ToList();
if (viewModel == null)
{
return HttpNotFound();
}
return View(viewModel);
}
视图模型:
public class ProdList
{
public int ProductID { get; set; }
public int ProductDescriptionID { get; set; }
public string Name { get; set; }
public decimal ListPrice { get; set; }
public string Description { get; set; }
}
详情查看:
@model AW_Internet.Models.ProdList
@{
ViewBag.Title = "Details";
}
<h2>Prod Details</h2>
<div class="displayField">
@Html.DisplayFor(model => model.ProductID)
</div>
<div class="displayField">
@Html.DisplayFor(model => model.ProductDescriptionID)
</div>
<div class="displayField">
@Html.DisplayFor(model => model.Name)
</div>
<div class="displayField">
@Html.DisplayFor(model => model.ListPrice)
</div>
<div class="displayField">
@Html.DisplayFor(model => model.Description)
</div>
<p>
@Html.ActionLink("Back to List", "Index")
</p>
我的索引中带有两个 id 的片段正在被解析:
<td>
@Html.ActionLink("Details", "Details", new { id1 = item.ProductID, id2 item.ProductDescriptionID })
</td>