In my controller i put model object to viewbag as shown in the below:
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);
}
}
}
In my view I render partial view and try to pass the object in the view. The passed object will be my new model in the partial view.
<div id="employee_files">
@{ Html.RenderPartial("_DetailsFiles", new { ViewBag.fileModel });
}
At my partial view i have this : @model G222.IK.Models.ViewModel.ViewFileModel
I'm getting the following exception. I think the is problem that ViewBag returns Object and the view is expecting ViewFileModel. I tried to cast it to my model in my renderer view but it did not work. Are there any fix for this?
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'.
You are passing a new anonymous object to the partial view.
@{ Html.RenderPartial("_DetailsFiles", new { ViewBag.fileModel });
creates a new object with a property fileModel that has the value of your fileModel object.
You should use:
@{ Html.RenderPartial("_DetailsFiles", ViewBag.fileModel);
that way the value of the ViewBag property will be passed correctly.