在我的控制器中,我将模型对象放入 viewbag,如下所示:
ViewBag.fileModel = new ViewFileModel(id)
public class ViewFileModel
{
public Employee Employee { get; set; }
public SelectList TrainingSelectList { get; set; }
public SelectList FileTypeSelectList { get; set; }
public int trainingId{ get; set; }
public int fileType { get; set; }
public ViewFileModel(int employeeId)
{
using (var unit = new UnitOfWork())
{
Employee = unit.EmployeeRepository.FindBy(e => e.Id == employeeId).SingleOrDefault();
if (Employee == null)
{
Debug.Print("Employee bulunamadi {0},", employeeId);
throw new ArgumentException("Hatali personel parametresi " + employeeId);
}
else
{
TrainingSelectList = new SelectList
(Employee.TrainingList, "Id", "TrainName");
FileTypeSelectList = SelectListUtils.FileTypeSelectList(1);
}
}
}
在我看来,我渲染部分视图并尝试在视图中传递对象。传递的对象将是我在局部视图中的新模型。
<div id="employee_files">
@{ Html.RenderPartial("_DetailsFiles", new { ViewBag.fileModel });
}
在我的部分观点中,我有这个:@model G222.IK.Models.ViewModel.ViewFileModel
我收到以下异常。我认为 ViewBag 返回 Object 并且视图期待 ViewFileModel 的问题。我试图在我的渲染器视图中将它投射到我的模型中,但它不起作用。有什么解决办法吗?
The model item passed into the dictionary is of type '<>f__AnonymousType0`1[System.Object]', but this dictionary requires a model item of type 'G222.IK.Models.ViewModel.ViewFileModel'.