I get an error on my View page thats says:
Cannot implicitly convert type ‘System.Collections.Generic.List’ to ‘SchoolAdministrator.SchoolAdminProductionServices.SchoolTypeRef’
I want to know how come Im getting these errors. I believe I am doing everything right
Controller
[SchoolAuthorizeAttribute(AdminRoles = "ViewSchoolTypeRef")]
public ActionResult ViewSchoolType()
{
try
{
Guid SchoolTypeRefId = Request["SchoolTypeRefId"] != null ? new Guid(Request["SchoolTypeRefId"]) : Guid.Empty;
ViewBag.merchantTypeRef = SchoolAdministrator.Models.SchoolAdminProduction.SchoolTypeRef.LoadSchoolTypeRef(SchoolTypeRefId, string.Empty, string.Empty, string.Empty);
}
catch (Exception e)
{
Commons.ErrorHandling.ReportError("SchoolAdministrator.Controllers.SchoolController ViewSchoolType",e);
}
return View();
}
Model
public class SchoolTypeRef
{
/// <summary>
/// This function allows us to load the School types if parametrs are empty, else load a single user by their ID
/// </summary>
/// <param name="SchoolTypeRef">The merchant type ref id, unique ID</param>
/// <param name="name">name of the School type</param>
/// <param name="description">description of the School type</param>
/// <returns>List of all users or list of a single user</returns>
public static List<SchoolAdminProductionServices.SchoolTypeRef> LoadSchoolTypeRef(Guid SchoolTypeRef, string name, string description, string fdrSchoolTypeCode)
{
try
{
SchoolAdminProductionServices.SchoolAdminProductionServicesSoapClient client = new SchoolAdminProductionServices.SchoolAdminProductionServicesSoapClient();
return client.LoadSchoolTypeRef(SchoolTypeRef, name, description, fdrSchoolTypeCode).ToList<SchoolAdminProductionServices.SchoolTypeRef>();
}
catch (Exception e)
{
Commons.ErrorHandling.ReportError("SchoolTypeRef.LoadSchoolTypeRef()",e);
}
return new List<SchoolAdminProductionServices.SchoolTypeRef>();
}
}
View
THIS IS WHERE THE ERROR OCCURRED BETWEEN THE <% %>
<%SchoolAdministrator.DarkstarAdminProductionServices.SchoolTypeRefmerchantTypeRef =
ViewBag.SchoolTypeRef ??
new SchoolAdministrator.SchoolAdminProductionServices.SchoolTypeRef();%>
<table>
<tr>
<td colspan="2" class="tableHeader"> School Type Ref Details</td>
</tr>
tr>
td class="label"> Name:</td>
td class="content"><%=SchoolTypeRef.name%></td>
/tr>
tr>
td class="label"> Description:</td>
td class="content"><%=SchoolTypeRef.description%></td>
/tr>
tr>
td class="label"> FDR SchoolType Code:</td>
td class="content"><%=SchoolTypeRef.fdrSchoolTypeCode%></td>
/tr>
/table>
Your web service call is returning a List<> of schoolRefTypes, but your view is trying to access the list as if it were a single instance of schoolRefTypes. Instead of assigning a list of schoolRefTypes to the viewbag, assign just one. A quick way to do this is use the .First() method to get the first element on the list. I’d imagine though you’d want to somehow restrict it to a specific schoolRefType based on the MVC convention of ID or something.