Asp.netmvc 5:不显示具有类型列表的强类型模型的部分视图



我通过J_Query调用局部视图控制器,控制器操作(ListInventoryProduct)被调用并执行,没有错误(列表被填充)。但部分视图不会显示。在浏览器开发工具说这是内部服务器错误。我搞不清楚问题出在哪里。以下是我的代码。

型号:

public class InventoryProductsViewModel
{
    public long Id { get; set; }
    [Display(Name = "Product Name")]
    public string Title { get; set; }
    [Display(Name = "SubCategory Name")]
    public string SubCategory { get; set; }
    [Display(Name = "Balance")]
    public int Balance { get; set; }
    [Display(Name = "Count")]
    public int InventoryCount { get; set; }
    [Display(Name = "Difference")]
    public string Difference { get; set; }
    [Display(Name = "IMEINumber")]
    public string IMEINumber { get; set; }
    [Display(Name = "BarrcodesString")]
    public string BarrcodesString { get; set; }
    public long subId { get; set; }
    // public List<Category> lstCategory { get; set; }
}

控制器动作

public ActionResult LoadInventoryProducts(long categoryId)
{
    Session["Products"] = null;
    Session["InventoryMissing"] = null;
    var userSession = Session.GetSessionUserInfo();
    if (userSession != null)
    {
        List<InventoryProductsViewModel> products = db.Products.Where(p => !p.IsDeleted && p.CompanyId == userSession.CompanyId && (categoryId == 0 || p.SubCategory.CategoryId == categoryId)).Select(p => new InventoryProductsViewModel { Id = p.Id, Title = p.Title, SubCategory = p.SubCategory.Title, IMEINumber = p.IMEINumber, Balance = (p.PurchasedQuantity - p.SoldQuantity) }).ToList(); //&& (subCategoryId == 0 || p.SubCategoryId == subCategoryId)
        Session["Products"] = products;
        if (Session["InventoryMissing"] == null)
        {
            Session["InventoryMissing"] = new List<InventoryMissing>(); 
            return PartialView("ProductsPartialView", products);
        }
        else
        {
            return Redirect("~/Error/Error");
        }
    }
}

PartialView

@model List<ViewModel.InventoryProductsViewModel>
<table >
    <tr>
        <th>@Html.DisplayNameFor(Model[0].Title)</th>
        <th>@Html.DisplayNameFor(Model[0].SubCategory)</th>
        <th>@Html.Label("Balance")</th>
        <th>@Html.Label("Count")</th>
        <th>@Html.Label("Difference")</th>
        <th>@Html.Label("IMEI Number")</th>
    </tr>
    @for (int i = 0; i < Model.Count(); i++ )
    {
        <tr id="@Model[i].Id">
            <td>
                @Html.Hidden(Model[i].subId)
                @Html.DisplayFor(Model[i].Title)
            </td>
            <td>@Html.DisplayFor(Model[i].SubCategory)</td>
            <td class="balance">@Html.DisplayFor(Model[i].Balance)</td>
            <td>@Html.EditorFor(Model[i].InventoryCount)</td>
            <td class="difference">0</td>
            <td>@Html.DisplayFor(modelItem =>Model[i].IMEINumber)</td>
        </tr>
    }
</table>

Html模型帮助程序被使用,这造成了问题。正确的帮助者如下。

@model List<HiTechPOS.ViewModel.InventoryProductsViewModel>
<table class=" table table-striped table-advance table-hover">
<tr>
    <th>
        @Html.Label("Product Name")
    </th>
    <th>
        @Html.Label("SubCategory Name")
    </th>
    <th>
       @Html.Label("Balance")
    </th>
    <th>
       @Html.Label("Count")
    </th>
    <th>
        @Html.Label("Difference")
    </th>
    <th>
        @Html.Label("IMEI Number")
    </th>
</tr>
@for (int i = 0; i < Model.Count(); i++ )
{
    <tr id="@Model[i].Id">
        @*<td>
            @Html.DisplayFor(modelItem => item.Id)
        </td>*@
        <td>
            @Html.Hidden(Model[i].subId.ToString())
            @Html.Label(Model[i].Title)
        </td>
        <td>
            @Html.Label(Model[i].SubCategory)
        </td>
        <td class="balance">
            @Html.Label(Model[i].Balance.ToString())
        </td>
        <td>
            @Html.Editor(Model[i].InventoryCount.ToString())
        </td>
        <td class="difference">
            @Html.Label(Model[i].Difference.ToString())
        </td>
        <td>
            if(Model[i].IMEINumber == null)
                {
                    @Html.Label("")
            }
            else
                { 
                @Html.Label(Model[i].IMEINumber)
            }
        </td>
    </tr>
}

最新更新