Asp.net mvc 5:没有显示类型列表的强类型模型的局部视图 [英] Asp.net mvc 5: Partial view with strongly typed model of type list is not displaying

查看:63
本文介绍了Asp.net mvc 5:没有显示类型列表的强类型模型的局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过J_Query调用部分视图控制器,调用控制器操作(ListInventoryProduct)并执行而不出错(列表已填充)。但是不显示局部视图。在浏览器开发工具中说它是内部服务器错误。我无法弄清楚问题是什么。

以下是我的代码。



型号:

I am calling partial view controller through J_Query, controller action(ListInventoryProduct) is called and execute without error( the list is populated). But the partial view is not displayed. In browser Developer tool says it is internal server error. I can't figure out what is the problem.
The following is my code.

Model:

 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; }
    ///// <summary>
    ///// categoryId
    ///// </summary>
    //public long CatId { get; set; }
    /// <summary>
    /// subCategory id
    /// </summary>
    public long subId { get; set; }
   // public List<category> lstCategory { get; set; }
}



========================= =====

控制器操作


==============================
Controller Action

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

            //if (products.Count() > 0)
            //{
            //    ViewBag.Category = products[0].SubCategory.CategoryId;
            //}
            //else
            //{
            //    ViewBag.Category = 0;
            //}

            return PartialView("ProductsPartialView", products);
        }

        else
        {
            return Redirect("~/Error/Error");

        }
    }



===================== ==============

PartialView


===================================
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>
    /pre>tr>
}</tr></table>

推荐答案

Html Helpers For Model were used which was creating problem. Correct Helpers are as follow.

<pre lang="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>
}</table></hitechpos.viewmodel.inventoryproductsviewmodel>


这篇关于Asp.net mvc 5:没有显示类型列表的强类型模型的局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆