如何渲染此部分视图将整数传递给我的ActionResult

时间:2013-12-02 17:13:15

标签: asp.net-mvc-4

这是我的ProductAttributeValueListAll表单,它显示正常,直到我尝试添加部分视图我收到以下错误:

传入字典的模型项的类型为'<> f__AnonymousType4 1[System.Int32]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1 [Nop.Admin.Models.Catalog.ProductModel + ProductVariantAttributeValueModel]'。

 @model IEnumerable<Nop.Admin.Models.Catalog.ProductModel.ProductVariantAttributeModel>

@{
    ViewBag.Title = "ProductAttributeValueListAll";
}

<h2>ProductAttributeValueListAll</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.ProductAttributeId)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ProductAttribute)
        </th>


    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.ProductAttributeId)
        </td>

        <td>
            @Html.DisplayFor(modelItem => item.ProductAttribute)
            <br />

          @Html.Partial("ProductAssociatedList", new { productVariantAttributeId = item.ProductAttributeId });

        </td>


    </tr>
}

这是我试图调用的部分视图控制器中操作的代码

    [ChildActionOnly]
public ActionResult ProductAssociatedList(int productVariantAttributeId)
{
    var values = _productAttributeService.GetProductVariantAttributeValues(productVariantAttributeId);

    var ProductVariantAttributeValue = values

        .Select(x =>
        {
             var associatedProduct = _productService.GetProductById(x.AssociatedProductId);
            //var pictureThumbnailUrl = _pictureService.GetPictureUrl(x.PictureId, 75, false);
            ////little hack here. Grid is rendered wrong way with <inmg> without "src" attribute
            //if (String.IsNullOrEmpty(pictureThumbnailUrl))
            //    pictureThumbnailUrl = _pictureService.GetPictureUrl(null, 1, true);
             var pvaModel = new ProductModel.ProductVariantAttributeValueModel()
             {
                 Id = x.Id,
                 ProductVariantAttributeId = x.ProductVariantAttributeId,
                 AttributeValueTypeId = x.AttributeValueTypeId,
                 AttributeValueTypeName = x.AttributeValueType.GetLocalizedEnum(_localizationService, _workContext),
                 AssociatedProductId = x.AssociatedProductId,
                 AssociatedProductName = associatedProduct != null ? associatedProduct.Name : "",
                 Name = x.ProductVariantAttribute.AttributeControlType != AttributeControlType.ColorSquares ? x.Name : string.Format("{0} - {1}", x.Name, x.ColorSquaresRgb),
                 IsPreSelected = x.IsPreSelected,
                     DisplayOrder = x.DisplayOrder

             };
                   // pvaModel.ViewEditUrl = Url.Action("ProductAssociatedList", "Product", new { productVariantAttributeId = x.Id });
                // pvaModel.ViewEditText = string.Format(_localizationService.GetResource("Admin.Catalog.Products.ProductVariantAttributes.Attributes.Values.ViewLink"), x.ProductVariantAttribute.ProductVariantAttributeValues != null ? x.ProductVariantAttribute.ProductVariantAttributeValues.Count : 0);
             return pvaModel;

        })
        .ToList();
    //var model =  ProductVariantAttributeValue.ToList();

    return View(ProductVariantAttributeValue);
}

这是我想插入的cshtml局部视图:

    @model IEnumerable<ProductModel.ProductVariantAttributeValueModel>

<p>

</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.AssociatedProductId)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.AssociatedProductName)
        </th>
        <th>

        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.AssociatedProductId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.AssociatedProductName)
        </td>
        <td>

        </td>
        <td>

        </td>
    </tr>
}

</table>

不确定我在这里做错了什么,有人得到了任何答案

1 个答案:

答案 0 :(得分:0)

您应该直接调用action方法,而不是部分视图。您调用Html.Partial的方式正在传递Int作为View的模型,而不是调用操作方法,因此:

@Html.Action("ProductAssociatedList", new { productVariantAttributeId = item.ProductAttributeId })

OR

@{Html.RenderAction("ProductAssociatedList", new { productVariantAttributeId = item.ProductAttributeId });}

参考:MSDN ChildActionExtensions.Action