更新部分视图MVC4

时间:2016-02-24 17:25:53

标签: asp.net-mvc-4 renderpartial html.actionlink

我有这个控制器:

public ActionResult PopulateTreeViewModel()
    {
        MainModelPopulate mainModelPopulate = new MainModelPopulate();
        // populate model
        return View(mainModelPopulate);
    }

有这样的观点:

@model xxx.xxx.MainModelPopulate
<table>
        @foreach (var item2 in Model.CountryList)
        {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item2.CountryName);
            </td>
        </tr>
            foreach (var item3 in item2.BrandList)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item3.BrandName);
                    </td>
                </tr>
                foreach (var item4 in item3.ProductList)
                {
                    <tr>
                        <td>
                         @Html.ActionLink(item4.ProductName, "FunctionX", new { idLab = item3.BrandID, idDep = item4.ProductID });                           
                    </td>
                    </tr>
                }
            }


        }
    </table>

FunctionX控制器是这样的:

public ActionResult FunctionX(int  idBrand=1 , int idProd=1)
    {
        List<ListTypeModel> typeModelList = new List<ListTypeModel>();
        // populate typeModelList
        return PartialView(typeModelList);
        }

    }

这个局部视图:

@model IEnumerable<TControl.Models.ListTypeModel>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>

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

        </tr>
    }

</table>

我想在主视图(PopulateTreeViewModel)中添加此部分视图,并使用函数X中包含的相对产品类型更新表。

我还尝试用@ Ajax.ActionLink替换@ Html.ActionLink,它的执行方式相同。

1 个答案:

答案 0 :(得分:0)

您是否尝试过@Html.RenderAction(item4.ProductName, "FunctionX", new { idLab = item3.BrandID, idDep = item4.ProductID });

还有其他选择..!请参考http://www.dotnet-tricks.com/Tutorial/mvc/Q8V2130113-RenderPartial-vs-RenderAction-vs-Partial-vs-Action-in-MVC-Razor.html