0

我在查看页面上收到错误消息:

无法将类型“System.Collections.Generic.List”隐式转换为“SchoolAdministrator.SchoolAdminProductionServices.SchoolTypeRef”

我想知道我是怎么得到这些错误的。我相信我做的一切都是正确的

控制器

 [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();
    }

模型

    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>();
    }
   }

查看这是 <% %> 之间发生错误的地方

   <%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>
4

3 回答 3

1

您的 Web 服务调用返回一个 SchoolRefTypes 的 List<>,但您的视图试图访问该列表,就好像它是 schoolRefTypes 的单个实例一样。与其将 schoolRefTypes 列表分配给 viewbag,不如只分配一个。一个快速的方法是使用 .First() 方法来获取列表中的第一个元素。我想虽然你想以某种方式将它限制为基于 ID 的 MVC 约定的特定 schoolRefType 或其他东西。

于 2012-07-03T14:42:15.657 回答
0

List 是一个通用列表。学校是您的自定义对象。asp.net 根本无法从您的类型转换为您的类型的通用列表。您应该尝试检查列表是否不为空,然后访问元素。

于 2012-07-03T14:43:29.177 回答
0

改变

<%SchoolAdministrator.DarkstarAdminProductionServices.SchoolTypeRefmerchantTypeRef =     ViewBag.SchoolTypeRef ?? new     SchoolAdministrator.SchoolAdminProductionServices.SchoolTypeRef();%>

<%SchoolAdministrator.DarkstarAdminProductionServices.SchoolTypeRefmerchantTypeRef SchoolTypeRef =     ViewBag.SchoolTypeRef ?? new     SchoolAdministrator.SchoolAdminProductionServices.SchoolTypeRef();%>
于 2012-07-03T14:44:08.810 回答