C# 平均方法无法首先使用asp.net核心代码:
2个模型类:C# 平均方法无法首先使用asp.net核心代码:,c#,asp.net-mvc,razor,entity-framework-core,average,C#,Asp.net Mvc,Razor,Entity Framework Core,Average,2个模型类: using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.ComponentModel.DataAnnotations; namespace CareCompProj.Models { public enum Profession { Counsellor, Physio, Doctor } public class H
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
namespace CareCompProj.Models
{
public enum Profession { Counsellor, Physio, Doctor }
public class HealthProfessional
{
public int ID { get; set; }
[Required]
[StringLength(50)]
[Display(Name = "Name")]
public string Name { get; set; }
public bool AcceptsMedicalCard { get; set; }
[Required]
[StringLength(80)]
[Display(Name = "Address")]
public string Address { get; set; }
[Required]
public Profession Profession { get; set; }
public ICollection<Review> HealthReviews { get; set; }
public double CalculateAverageRating()
{
var averageRating = HealthReviews.Average(r => r.Rating);
return averageRating;
}
public ICollection<Client> Clients { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
namespace CareCompProj.Models
{
public class Review
{
public int ID { get; set; }
public int? ClientID { get; set; }
public int? HealthProfessionalID { get; set; }
[Required]
[Range(0, 5, ErrorMessage = "Please select number between 1 and 5")]
public int Rating { get; set; }
[Required]
public string ReviewText { get; set; }
public bool WouldRecommend { get; set; }
public virtual Client Client { get; set; }
public HealthProfessional HealthProfessional { get; set; }
}
}
专业卫生人员使用的razor索引视图
@model IEnumerable<CareCompProj.Models.HealthProfessional>
@model IEnumerable
在此方面的任何帮助都将不胜感激。谢谢在设置
ViewBag
或使用返回视图(模型)
从请求返回之前,您实际上需要在控制器操作中调用ToList()
来具体化结果,以便执行查询并将结果集填充到列表中的内存中
因此,对于linq查询,请执行以下操作:
var model = _context.HealthProfessionals.Include(p => p.HealthReviews)
.Select(x => new
{
HealthProfessional = x,
RatingAverage = x.HealthReviews.Any() ?
x.HealthReviews.Average(v => (v.Rating)) : 0
}).ToList();
现在,返回时将其传递回视图,如下所示:
return View(model);
或者您可以设置ViewBag.profRatings
,然后在视图中使用它
更新:
可以创建特定于视图要求的视图模型,如:
public class HealthProfessionalVM
{
public HealthProfessional HealthProfessional { get; set; }
public int RatingAverage { get ;set; }
}
然后在控制器操作中填充它,而不是创建匿名对象:
var vm = _context.HealthProfessionals.Include(p => p.HealthReviews)
.Select(x => new HealthProfessionalVM
{
HealthProfessional = x,
RatingAverage = x.HealthReviews.Any() ?
x.HealthReviews.Average(v => (v.Rating)) : 0
}).ToList();
return View(vm);
现在将视图强式键入到IEnumerable
,然后像这样迭代:
@model IEnumerable<HealthProfessionalVM>
foreach(var item in Model)
{
<p> @item.RatingAverage </p>
}
@model IEnumerable
foreach(模型中的var项目)
{
@item.RatingAverage
}
希望有帮助。在设置视图包之前,您实际上需要在控制器操作中调用ToList()
来具体化结果,或者使用返回视图(模型)
从请求返回,以便执行查询并将结果集填充到列表中的内存中
因此,对于linq查询,请执行以下操作:
var model = _context.HealthProfessionals.Include(p => p.HealthReviews)
.Select(x => new
{
HealthProfessional = x,
RatingAverage = x.HealthReviews.Any() ?
x.HealthReviews.Average(v => (v.Rating)) : 0
}).ToList();
现在,返回时将其传递回视图,如下所示:
return View(model);
或者您可以设置ViewBag.profRatings
,然后在视图中使用它
更新:
可以创建特定于视图要求的视图模型,如:
public class HealthProfessionalVM
{
public HealthProfessional HealthProfessional { get; set; }
public int RatingAverage { get ;set; }
}
然后在控制器操作中填充它,而不是创建匿名对象:
var vm = _context.HealthProfessionals.Include(p => p.HealthReviews)
.Select(x => new HealthProfessionalVM
{
HealthProfessional = x,
RatingAverage = x.HealthReviews.Any() ?
x.HealthReviews.Average(v => (v.Rating)) : 0
}).ToList();
return View(vm);
现在将视图强式键入到IEnumerable
,然后像这样迭代:
@model IEnumerable<HealthProfessionalVM>
foreach(var item in Model)
{
<p> @item.RatingAverage </p>
}
@model IEnumerable
foreach(模型中的var项目)
{
@item.RatingAverage
}
希望有帮助。嗨,谢谢你的回答。我已将其应用于我的控制器,并将其分配给ViewBag.profRatings=model;接下来,我在我的索引视图中使用@foreach(模型中的var项){@ViewBag.profRatings}调用它。不幸的是,我仍然得到System.Collections.Generic.List1[f_uAnonymousType2
2[CareCompProj.Models.HealthProfessional,System.Double],其中平均数应该在我的表中。有什么想法吗?您需要编写:foreach(ViewBag.profRatings中的var项为IEnumerable){@item.RatingAverage
状态healthprofessional不包含RatingAverage的定义,如果我在razor视图中这样做,则评级平均值用红色下划线标出。这是您的意思吗?您需要的不是匿名对象,而是视图模型classI对您的答案应用了最新的更新,只需进行一些轻微的编辑,以匹配我的项目我已经在我的控制器中应用了它,并将其分配给ViewBag.profRatings=model;接下来,我在索引视图中使用@foreach(模型中的var项){@ViewBag.profRatings}调用了它。不幸的是,我仍然得到System.Collections.Generic.List1[f_uAnonymousType2
2[CareCompProj.Models.HealthProfessional,System.Double]]平均数应该在我的表格中。有什么想法吗?你需要写:foreach(ViewBag.profRatings中的var项为IEnumerable){@item.RatingAverage
状态healthprofessional不包含RatingAverage的定义,如果我在razor视图中这样做,则评级平均值用红色下划线标出。这是您的意思吗?您需要的不是匿名对象,而是视图模型classI对您的答案应用了最新的更新,只需进行一些轻微的编辑,以匹配我的项目如果你得到一个ArgumentNullException
,那是因为HealthReviews
是空的
-确保在将模型传递到视图之前填充它(不要使用ViewBag
)如果您得到的是ArgumentNullException
,这是因为HealthReviews
是null
-请确保在将模型传递到视图之前填充它(并且不要使用ViewBag
)