简体   繁体   中英

MVC 5 Model Child list can't render in Partial view

I am new to ASP.NET MVC 5 . On debug mode, I saw some values being looped to the partial view but in UI no values are being displayed.

My Model

public class Model
{
    public int ID { get; set; }

    public string UserID { get; set; }

    public List<ReferenceModel> ReferenceModelList{ get; set; }
}

My Controller

public ActionResult GetModel(string dataobject, int id = 0)
{
    Model model = new Model();
    model = BL.GetModel(dataobject, id);
    return PartialView("_ReferenceModelList", model);
}

ReferenceModelList(PartialView)

@model Web.Model.Model
@{
    Layout = null;
}

@foreach (var menurefitem in Model.ReferenceModelList)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => menurefitem.Code)
        </td>
        <td>
            @Html.DisplayFor(modelItem => menurefitem.Description)
        </td>
    </tr>
}

Partial View Data in VS: 在此处输入图片说明

Please help.

您需要像这样从主UI调用Controller方法

@{Html.RenderAction("GetModel","SAPSecurity");}

At first you should instantiate your list at the constructor of your class.I think your problem should be solved.

public class Model
{
    public Model()
    {
        ReferenceModelList=new List<ReferenceModel>();
    }
    public int ID { get; set; }

    public string UserID { get; set; }

    public List<ReferenceModel> ReferenceModelList { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM