[英]'object' does not contain a definition for 'rxDetail'
Controller code 控制器代码
public ActionResult Index()
{
var data = (from pd in db.Prescription_Details join md in db.Medicine_Details
on pd.Medicine_Id equals md.Medicine_Id
select new MyPrescription {rxDetail=pd.Rx_Detail_Id , medicineName=md.Medicine_Name , dosageFrequency= pd.Dosage_Frequency}).ToList();
var prescs = db.Prescription_Details.Select(x => x.Prescription.Doctor_Details.Doctor_Name).Distinct().ToList();
//entity
ViewBag.prescriptions = prescs;
ViewBag.dd = data;
return View();
}
Razor code
@foreach (var item in ViewBag.dd)
{
<tr>
<td>@item.rxDetail</td>
<td>@item.medicineName</td>
<td>@item.dosageFrequency</td>
</tr>
}
Apart from making new class MyPrescriptions I am getting this error . 除了创建新类MyPrescriptions之外,我还会遇到此错误。 Is there some other way to proceed with it ? 还有其他方法可以继续吗?
Is your MyPrescription
class internal? 您的MyPrescription
类是内部类吗? Razor views are compiled into a separate assembly, so internal classes are not accessible there. 剃刀视图被编译为单独的程序集,因此内部类无法在其中访问。
Trying to access properties of an inaccessible class from a dynamic
variable throws the exception you get. 尝试从dynamic
变量访问无法访问的类的属性会引发异常。
ViewBag
internally stores defined properties value as Object. ViewBag
内部将定义的属性值存储为Object。 It is actually a dynamic type defined by ExpandoObject that stored the data as dictionary. 它实际上是ExpandoObject定义的动态类型,将数据存储为字典。 Once you convert any custom object
to type object
then it won't know underlying properties of custom object unless you explicitly cast it. 一旦将任何custom object
转换为类型object
,除非明确地将其强制转换,否则它将不知道自定义对象的基础属性。
So all you need to do is cast it to your custom object before using: 因此,您需要做的就是将其转换为自定义对象,然后再使用:
@foreach (var item in ViewBag.dd as MyPrescription) @foreach(ViewBag.dd中的变量项作为MyPrescription)
@foreach (MyPrescription item in ViewBag.dd)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.