1

谁能想到为什么当我单击详细信息链接时我没有得到所需的输出?我得到错误: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>
4

3 回答 3

4

如果您希望ProdList查询返回多个项目,请将您的视图更新为:

@model List<AW_Internet.Models.ProdList>

并更新您在视图中引用所有内容的方式。您更有可能尝试返回单个记录,因此您应该编写:

ProdList prodList = db.Database.SqlQuery<ProdList>(test).FirstOrDefault();

而且您可能还想更新您的方法,因为它永远不会等于您正在检查的位置,如果是的话,它会在您编辑它null时引发异常。.ToList()

public ActionResult Details(int id1 = 0, int id2 = 0)
{
    string test = "...";

    var viewModel = db.Database.SqlQuery<ProdList>(test).ToList();

    if (!viewModel.Any())
        return HttpNotFound();

    return View(viewModel);          
}

或更可能的情况是您只需要 1 条记录:

public ActionResult Details(int id1 = 0, int id2 = 0)
{
    string test = "...";

    ProdList prodList = db.Database.SqlQuery<ProdList>(test).FirstOrDefault();

    if (prodList == null)
        return HttpNotFound();

    return View(prodList);          
}

也许也尝试更新您的变量名称 - :)

于 2013-08-05T15:32:06.287 回答
3

在您的 Details 方法中,您返回了 a List<ProdList>,而在您的视图中,您声明了 a 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.First());          
}

希望能帮助到你

于 2013-08-05T15:25:16.030 回答
2

db.Database.SqlQuery<ProdList>(test)返回一个集合,您的页面需要一个元素。你可以这样做:

var viewModel = db.Database.SqlQuery<ProdList>(test).FirstOrDefault();
if (viewModel == null)
{
    return HttpNotFound();
}
return View(viewModel)); 
于 2013-08-05T15:26:34.513 回答