Inside my model class I am using a method call IsAlreadyAssigned, as follow:-
在我的模型类中,我使用方法调用IsAlreadyAssigned,如下所示: -
public partial class DataCenter
{
public bool IsAlreadyAssigned()
{
return (TMSRacks.Any() || TMsRouters.Any() || Zones.Any());
}
}
The main aim of this helper method is to hide the delete button on the view if the object have child records; as follow:-
这个辅助方法的主要目的是在对象具有子记录时隐藏视图上的删除按钮;如下:-
<td>
@Html.ActionLink("Edit", "Edit", new { id= item.ID})
@if (!item.IsAlreadyAssigned())
{
<text>|</text>
@Ajax.ActionLink("Delete",
"Delete", "DataCenter",
new { id = item.ID },
new AjaxOptions
{
Confirm = "Are You sure You want to delete (" + item.Name + ")",
HttpMethod = "Post",
OnSuccess = "deletionconfirmation",
OnFailure = "deletionerror"
})
}
</td>
But on my index view which shows 10 records at a time,I will hide or display the delete link accordingly ,so I have to include all the navigation properties, in my query as follow:-
但是在我一次显示10条记录的索引视图中,我将相应地隐藏或显示删除链接,因此我必须在我的查询中包含所有导航属性,如下所示: -
public IQueryable<DataCenter> AllFindDataCenters(string q, bool forautocomplete = false)
{
return from datacenter in tms.DataCenters.Where(a=> (String.IsNullOrEmpty(q)) || ( a.Name.ToUpper().StartsWith(q.ToUpper())))
.Include(a=>a.Zones)
.Include(a=>a.TMsRouters)
.Include(a=>a.TMSRacks)
select datacenter;
}
Other wise each record on my index view might make at most three queries to the DB to check if there is any child records. So I ended up including all the navigation properties as shown above, just to implement the requirement of hiding /displaying a delete link . so is there a better way to manage my logic, as I do not need to display any of the navigation properties data (tmsrouter,tmsfirewalls,zonea), I just want to know if atleat one record exists or not ? Thanks
另外,我的索引视图上的每条记录最多可以向DB发出三个查询,以检查是否有任何子记录。所以我最终包含了如上所示的所有导航属性,只是为了实现隐藏/显示删除链接的要求。那么有没有更好的方法来管理我的逻辑,因为我不需要显示任何导航属性数据(tmsrouter,tmsfirewalls,zonea),我只想知道是否存在一条记录?谢谢
1 个解决方案
#1
2
Instead of working directly with your business models in your view, return a view model and expose the information you need e.g.
不要直接在视图中使用您的商业模式,而是返回视图模型并公开您需要的信息,例如:
public class DataCenterViewModel
{
...
public bool HasZones { get; set; }
public bool HasTmsRouters { get; set; }
public bool HasTmsRacks { get; set; }
public bool AlreadyAssigned { get { return HasZones || HasTmsRouters || HasTmsRacks; } }
}
Then in your query construct your view models. Also, just an FYI, it's best to work with a more generic construct like IEnumerable<T>
when passing stuff to the view, IQueryable<T>
suggests you want to further query the data source once at the view.
然后在您的查询中构建您的视图模型。另外,仅仅是一个FYI,最好在向视图传递内容时使用更通用的构造,如IEnumerable
public IEnumerable<DataCenterViewModel> AllFindDataCenters(string q, bool forautocomplete = false)
{
return tms.DataCenters.Where(...)
.Select(x => new DataCenterViewModel
{
...
HasZones = x.Zones.Any(),
HasTmsRouters = x.TMSRouters.Any(),
HasTmsRacks = x.TMSRacks.Any()
})
.ToList();
}
Then finally in your view
最后在你看来
<td>
@Html.ActionLink("Edit", "Edit", new { id= item.ID})
@if (!item.AlreadyAssigned)
{
...
}
</td>
#1
2
Instead of working directly with your business models in your view, return a view model and expose the information you need e.g.
不要直接在视图中使用您的商业模式,而是返回视图模型并公开您需要的信息,例如:
public class DataCenterViewModel
{
...
public bool HasZones { get; set; }
public bool HasTmsRouters { get; set; }
public bool HasTmsRacks { get; set; }
public bool AlreadyAssigned { get { return HasZones || HasTmsRouters || HasTmsRacks; } }
}
Then in your query construct your view models. Also, just an FYI, it's best to work with a more generic construct like IEnumerable<T>
when passing stuff to the view, IQueryable<T>
suggests you want to further query the data source once at the view.
然后在您的查询中构建您的视图模型。另外,仅仅是一个FYI,最好在向视图传递内容时使用更通用的构造,如IEnumerable
public IEnumerable<DataCenterViewModel> AllFindDataCenters(string q, bool forautocomplete = false)
{
return tms.DataCenters.Where(...)
.Select(x => new DataCenterViewModel
{
...
HasZones = x.Zones.Any(),
HasTmsRouters = x.TMSRouters.Any(),
HasTmsRacks = x.TMSRacks.Any()
})
.ToList();
}
Then finally in your view
最后在你看来
<td>
@Html.ActionLink("Edit", "Edit", new { id= item.ID})
@if (!item.AlreadyAssigned)
{
...
}
</td>