如何显示嵌套列表并维护MVC模式? [英] How to display nested list and maintain MVC pattern?
问题描述
尝试使用MVC 6和EF代码优先设计显示嵌套列表.我正在使用
现在,我想在每个位置下方显示子位置,如下图所示.用户可以添加新位置或绑定到该位置的子位置.
显示位置列表很简单: return View(_context.Location.ToList());
要显示嵌套的子位置列表,我应该在控制器或视图中进行工作吗?
我希望我可以像下面这样使用视图,但是 item.Sublocations
是 null
,原因是我不确定,因为其中包含数据数据库.
@foreach(模型中的变量项){< tr>< td> @ Html.DisplayFor(modelItem => item.LocationName)</td></tr>@foreach(item.Sublocations中的SubLocation itm){< tr>< td> @ itm.SubLocationName</td></tr>}}
我尝试在控制器中创建查询,但是无法使用外键进行比较.
var test =(从_context.SubLocation中的m其中m.LocationID == curLocID<-m.LocationID不可用选择m.SubLocationName).ToList();
即使我可以使用LocationID,也不确定如何将当前View(curLocID)从视图"发送回控制器.我想我需要一个辅助方法,但是我开始盘算了.
如何维护适当的MVC并显示主类的子类中的嵌套列表?
您应该考虑将 LocationID
属性添加到您的 SubLocation
类中,该属性将是 Location
表.
公共类SubLocation{public int ID {get;放;}公共字符串SubLocationName {get;放;}public int LocationID {set; get;}}
现在,查询位置.位置具有 SubLocations
属性,因此您的视图应该可以正常工作.
var location = _context.Locations.Include(s => s.SubLocations).ToList();返回View(位置);
Attempting to display nested list using MVC 6 and EF code first design. I'm using this Tutorial to get me started with MVC and am trying to take it to another level.
Classes:
public class Location
{
[Key]
public int ID { get; set; }
public string LocationName { get; set; }
public ICollection<SubLocation> Sublocations { get; set; }
}
}
public class SubLocation
{
public int ID { get; set; }
public string SubLocationName { get; set; }
}
Running dnx ef database update
sets up my EF database correctly and assigns a foreign key to LocationID
under SubLocation
.
Now I want to display Sub Locations under each Location like the following image. The user can add new locations or sub locations tied to a location.
Displaying the list of locations is simple: return View(_context.Location.ToList());
To display the nested list of sub-locations, should I do the work in the controller or view?
I was hoping I could just use the view like the following but item.Sublocations
is null
for a reason that I'm not sure of since there is data in the database.:
@foreach (var item in Model) {
<tr>
<td>@Html.DisplayFor(modelItem => item.LocationName)</td>
</tr>
@foreach (SubLocation itm in item.Sublocations)
{
<tr>
<td>@itm.SubLocationName</td>
</tr>
}
}
I've tried creating a query in the controller but the foreign key isn't available to compare against.
var test = (from m in _context.SubLocation
where m.LocationID == curLocID <-- m.LocationID isn't available
select m.SubLocationName
).ToList();
Even if I could use the LocationID I'm not sure how to send the current Location (curLocID) from the View back to the controller. I think I need a helper method but I'm starting to go in circles.
How can I maintain proper MVC and display nested list from a subclass of my main class?
You should consider adding a LocationID
property to your SubLocation
class which will be the foreign key to the Location
table.
public class SubLocation
{
public int ID { get; set; }
public string SubLocationName { get; set; }
public int LocationID { set;get;}
}
Now, query the locations. Location has a SubLocations
property,so your view should work fine.
var locations = _context.Locations.Include(s=>s.SubLocations).ToList();
return View(locations);
这篇关于如何显示嵌套列表并维护MVC模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!