这是我的方法
public PartialViewResult GetAllClassroom()
{
Guid entityId = new Guid();
var searchClassRooms = from m in db.Forms
where m.FormTypeEnumId == "CLASSROOM" && m.FormTitle.ToUpper() == "Add a Classroom".ToUpper() && m.EntityId == entityId
join fs in db.FormSubmits on m.FormId equals fs.FormId
select new
{
FormId = m.FormId,
FormSubmitsId = fs.FormSubmitId,
FormTitle = fs.FormTitle,
CreatedOn = fs.CreatedOn
};
return PartialView("_getClassRoom", searchClassRooms.Distinct().OrderByDescending(s => s.CreatedOn).ToList());
}
我想在我的视图中传递这些值但是它说没有传递给视图的模型...视图如下
@foreach (var item in Model)
{
<p>FormId: </p> @item.FormId
<p>FormSubmitsId : </p> @item.FormSubmitsId
<p>FormTitle : </p> @item.FormTitle
<p>CreatedOn : </p> @item.CreatedOn
}
我做错了什么?
答案 0 :(得分:1)
执行以下操作
1-创建一个类Result,如下所示
public class Result
{
public int FormId{get; set;}
public int FormSubmitsId{get; set;}
public string FormTitle{get; set;}
public DateTime CreatedOn{get; set;}
}
2-将控制器中的代码更改为以下
public PartialViewResult GetAllClassroom()
{
Guid entityId = new Guid();
var searchClassRooms = from m in db.Forms
where m.FormTypeEnumId == "CLASSROOM" && m.FormTitle.ToUpper() == "Add a Classroom".ToUpper() && m.EntityId == entityId
join fs in db.FormSubmits on m.FormId equals fs.FormId
select new Result // adding the Result to be strongly typed view
{
FormId = m.FormId,
FormSubmitsId = fs.FormSubmitId,
FormTitle = fs.FormTitle,
CreatedOn = fs.CreatedOn
};
return PartialView("_getClassRoom", searchClassRooms.Distinct().OrderByDescending(s => s.CreatedOn).ToList());
}
3-将您的观点修改为以下
@model IEnumerable<YourSolutionName.Models.Result> // you might change the path of your Result class
@foreach (var item in Model)
{
<p>FormId: </p> @item.FormId
<p>FormSubmitsId : </p> @item.FormSubmitsId
<p>FormTitle : </p> @item.FormTitle
<p>CreatedOn : </p> @item.CreatedOn
}
<强> EIDTED 强>
保持您的设置,这意味着将匿名对象传递给视图
然后你可以用这种方式使用反射渲染视图
@model dynamic
@{
var type = Model.GetType().GetProperties();
}
@foreach (System.Reflection.PropertyInfo property in type)
{
<p>@(property.Name) :</p>@property.GetValue(Model, null)
}
希望这会对你有所帮助