我想在同一视图mvc5 c#
中检索父和子的数据<ul class="circle2">
<li style=" border:0px">
<div class="text">@Html.DisplayFor(model => model.Name)</div>
</li>
<ul class="circle3">
@foreach (var item1 in Model)
{
<li>
<div class="text">@Html.Display(modelItem => item1.Name)</div>
</li>
}
<li>
<div class="text" data-toggle="tooltip" data-placement="left" title="add family member">@Html.ActionLink("+", "Create")</div>
</li>
答案 0 :(得分:0)
您的模型必须如下所示:
public class newtree
{
public string User_ID { get; set; }
public string Name { get; set; }
public string perantID { get; set; }
List<Child> Children { get; set; }
}
您需要使用正确的数据填充子列表,然后使用@foreach(Model.Children中的var item1)进行迭代
答案 1 :(得分:0)
型号
public class EmployeeViewModel {
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public EmployeeDepartment employeeDepartment { get; set; }
}
public class EmployeeDepartment {
public int Id { get; set; }
public string DepartmentName { get; set; }
}
控制器
public ActionResult EmployeeData() {
EmployeeViewModel model = new EmployeeViewModel();
EmployeeDepartment ed = new EmployeeDepartment();
model.Id = 1;
model.Name = "Muhammmad Ashraf Faheem";
model.Email = "it.ashraffaheem@gmail.com";
ed.Id = 1;
ed.DepartmentName = "Development";
model.employeeDepartment = ed;
return View(model);
}
查看
@model UploadFileDemo.Models.EmployeeViewModel
@{ViewBag.Title = "EmployeeData";}
<label>@Model.Id</label><br />
<label>@Model.Name</label><br />
<label>@Model.Email</label><br />
<label>@Model.employeeDepartment.Id</label><br />
<label>@Model.employeeDepartment.DepartmentName</label>
通过这种方式,我们可以在同一视图中使用父模型和子模型: