任何人都可以想到爲什麼我沒有得到所需的輸出,當我點擊詳細信息鏈接? 我得到的錯誤: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'.
MVC 4詳細信息頁面正在解析2 id的
控制器:
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>
這解決了它的感謝!它是否是用於對動作結果編輯進行排序的同一種方法?只是將查詢過程更改爲更新 – Chris
您必須返回與視圖上聲明的相同模型。在你的例子中,你返回了一個'List',你查看使用'ProdList' –