MVC 4详细信息页面正在解析2 id';s

本文关键字:id 详细信息 MVC | 更新日期: 2023-09-27 18:26:26

当我点击详细信息链接时,有人能想到为什么我没有得到所需的输出吗?我得到错误: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);          
}

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>

MVC 4详细信息页面正在解析2 id';s

如果您希望查询返回多个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);          
}

或者更可能的情况是,你只想要一条记录:

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);          
}

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

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

希望它能帮助

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

var viewModel = db.Database.SqlQuery<ProdList>(test).FirstOrDefault();
if (viewModel == null)
{
    return HttpNotFound();
}
return View(viewModel));