我有一个动作结果,用于从我的View Model填充IQueryable字段和IEnumerable字段。我可以让一个或另一个被填充,但不能同时遇到这个错误。我需要填充两个字段,然后将它们传递给视图。任何人都可以帮我解决这个错误吗?
查看型号:
public class AdminReviewViewModel {
public IEnumerable<Review> Reviews { get; set; }
public IQueryable Clients { get; set; }
public int ClientID { get; set; }
public int ReviewID {get; set;}
public string ReviewName { get; set; }
public int? ReviewPeriodID { get; set; }
public string ReviewPeriodName { get; set; }
}
控制器:
public ActionResult Review() {
AdminReviewViewModel model = new AdminReviewViewModel();
model.Clients = clientRepo.Clients;
var reviews = reviewRepo.GetAllReviews();
model = reviews.Select(s => new AdminReviewViewModel() {
ClientID = s.ClientID,
ReviewID = s.ReviewID,
ReviewName = s.ReviewName,
ReviewPeriodID = s.ReviewPeriodID,
ReviewPeriodName = s.ReviewPeriod,
});
return View(model);
}
查看:
<div class="table table-responsive" id="TablePanel">
<table class="table table-bordered"id="data">
<thead class="TableHead">
<tr>
<td>
Client ID
</td>
<td>
Review ID
</td>
<td>
Review Name
</td>
<td>
Review Period ID
</td>
<td>
Review Period Name
</td>
<td></td>
</tr>
</thead>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ClientID)
</td>
<td>
@Html.DisplayFor(modelItem => item.ReviewID)
</td>
<td>
@Html.DisplayFor(modelItem => item.ReviewName)
</td>
<td>
@Html.DisplayFor(modelItem => item.ReviewPeriodID)
</td>
<td>
@Html.DisplayFor(modelItem => item.ReviewPeriodName)
</td>
<td>
Edit
</td>
</tr>
}
</table>
</div>
答案 0 :(得分:2)
model = reviews.Select(s => new AdminReviewViewModel() {
ClientID = s.ClientID,
ReviewID = s.ReviewID,
ReviewName = s.ReviewName,
ReviewPeriodID = s.ReviewPeriodID,
ReviewPeriodName = s.ReviewPeriod,
});
表示“让model
调用Select
”的结果。由于model
已被定义为单个AdminReviewViewModel
,因此这没有意义。
可能你希望这更像是:
public class AdminReviewViewModel
{
public int ClientID { get; set; }
public int ReviewID {get; set;}
public string ReviewName { get; set; }
public int? ReviewPeriodID { get; set; }
public string ReviewPeriodName { get; set; }
}
public class AdminReviewListViewModel
{
public IEnumerable<Review> Reviews { get; set; } // Don't know what this is for, but leaving it.
public IQueryable Clients { get; set; }
public IQueryable<AdminReviewViewModel> ReviewList { get; set; }
}
public ActionResult Review()
{
AdminReviewListViewModel model = new AdminReviewListViewModel();
model.Clients = clientRepo.Clients;
var reviews = reviewRepo.GetAllReviews();
model.ReviewList = reviews.Select(s => new AdminReviewViewModel() {
ClientID = s.ClientID,
ReviewID = s.ReviewID,
ReviewName = s.ReviewName,
ReviewPeriodID = s.ReviewPeriodID,
ReviewPeriodName = s.ReviewPeriod,
});
return View(model);
}
然后你有一个包含你选择的结果的模型。
答案 1 :(得分:0)
我认为您的reviews
是某种类型的IEnumerable<>
,您可以使用您的linq选择IEnumerable<AdminReviewViewModel>
。
model = reviews.Select(s => new AdminReviewViewModel() {
ClientID = s.ClientID,
ReviewID = s.ReviewID,
ReviewName = s.ReviewName,
ReviewPeriodID = s.ReviewPeriodID,
ReviewPeriodName = s.ReviewPeriod,
}).FirstOrDefault();
如果您只需要第一个元素,或者您必须将模型类型更改为IEnumerable<AdminReviewViewModel>
。
<强>更新强>
public ActionResult Review() {
var reviews = reviewRepo.GetAllReviews();
var models = reviews.Select(s => new AdminReviewViewModel() {
ClientID = s.ClientID,
ReviewID = s.ReviewID,
ReviewName = s.ReviewName,
ReviewPeriodID = s.ReviewPeriodID,
ReviewPeriodName = s.ReviewPeriod,
Clients = clientRepo.Clients
});
return View(models);
}