Transfer repository data to MVC Model
I am getting an error
Error 826 Cannot implicitly convert type 'System.Collections.Generic.IE开发者_运维百科numerable' to 'OVR.Models.ChildInfoModel'. An explicit conversion exists (are you missing a cast?) C:\dev.....\Controllers\NewRecordController.cs 129 34 OVR
public ActionResult ChildInfo(ChildInfoModel childInfoModel)
{
//INewRecord newRecord = (INewRecord)TempData["newRecord"];
if (TempData["CertNum"].ToString() == "")
{
return View();
}
else
{
string certNum = (string)TempData["CertNum"];
//call up Child Info Screen
var repo = new Get_Home_Repository();
childInfoModel = repo.Get_Screen_DataDS(certNum, "ChildInfo");
//childInfoModel = cModel;
return View(childInfoModel);
}
}
- TempData["CertNum"] contains the value I need
- The View is strongly typed to the model with @model OVR.Models.ChildInfoModel
- I added in the parameter (ChildInfoModel childInfoModel) but if a link is not clicked a blank page is to be pulled up and allowed to be filled out, otherwise I pass in the parameters needed to pull data from a repository which it then populates the interface IChildInfo of which contains the data from a list as IEnumerable. I figured I could somehow explicitly convert .. or that the Interface IChildInfo needs to get updated in order to pass back the model to the view. How do I get the model populated from the repository data?
What is Get_Screen_DataDS returning? Whats the method signature return? It seems you are returning IEnumerable from Get_Screen_DS - what is this an IEnumerable of? You are assigning to an instance of childInfoModel which I'm assuming is not an IEnumerable but I can't really tell from what you have posted.
Should you be returning an IEnumerable? If ChildInfoModel implements IEnumerable then just simply cast it
childInfoModel = (ChildInfoModel) repo.Get_Screen_DataDS(certNum, "ChildInfo"); However in this case you should probably just hat Get_Screen_DataDS return type ChildInfoModel.
Also, it will help you much in the long haul to stick to consistent and standard .net naming conventions.
var repo would be var repository. GetScreenDataDS would be.... GetScreenData() or GetScreenDataByCert() FxCop rules can help you with this : ) Just my extra .00000002
精彩评论