Alex Rivera | Logout

Unable to cast the type 'System.Nullable`1' to type 'System.Object' - ASP.NET MVC

Asked 2012-07-05T05:48:04.460
10

ERROR

Unable to cast the type 'System.Nullable`1' to type 'System.Object'.
 LINQ to Entities only supports casting Entity Data Model primitive types.

This is the error I am getting.

Controller -

 public ActionResult FixturesAll()
 {
     teamMgr = new TeamManager();
     fixtureMgr = new FixtureManager();

     var team = teamMgr.GetTeams();
     var viewModel = new TeamIndexViewModel()
     {
          Teams = team.ToList(),
          NumberOfTeams = team.Count()
     };

     var fixtures = from fixtures in Oritia_entities.Fixtures
                    where fixtures.SeasonId == seasionID
                    select new FixtureModel
                    {

                        Team1 = "",
                        Team2 = "",
                        Winners = (fixtures.TeamWon+""),
                        FirstBattingTeam = (fixtures.FirstBattingTeam+""),
                        SecondBattingTeam = (fixtures.SecondBattingTeam+""),
                        Team1Score = fixtures.Team1Score + "",
                        Team1Wickets = fixtures.Team1Wickets + "",
                        Team2Score = fixtures.Team2Score + "",
                        Team2Wickets = fixtures.Team2Wickets + ""
                    };

      ViewData["Fixtures"] = fixtures;
      return View(viewModel);
 }

Partial View

<%@ Control Language="C#" Inherits=
"System.Web.Mvc.ViewUserControl<IEnumerable<DataAccess.FixtureModel>>" %>
    <table>
    <% foreach (var item in ViewData["Fixtures"]
 as IEnumerable<DataAccess.FixtureModel>) // Here I am getting the error
       { %>
          <tr>                 
            <td>
                <%: item.Team1 %>
            </td>
            <td>
                <%: item.Team2 %>
            </td>
          </tr>
   </table&
Edit
Report

2 Answers

0

Use this in the controller:

  ViewData["Fixtures"] = fixtures.ToArray();

I'm not sure, but I think that's where your issue is. If you don't use ToArray, then you're sending an IQueryable to the View, which could be problematic.

answered 2012-07-05T05:59:10.630
0

Give this a shot... (although id tweak the try catch a bit, if you end up using it. i wouldn't recommend suppressing an error and not doing anything about it/allow it to die a silent death ;P)

 public string NullableObjectToString(object obj)
 {
      try
      {
          if (obj == null)
              return "";

          return (obj ?? string.Empty).ToString();
      }
      catch (Exception)
      {
          return "";
      }
  }

Then

var fixtures = from fixtures in Oritia_entities.Fixtures
                  where fixtures.SeasonId == seasionID
                  select new FixtureModel
                  {                        
                     Winners = NullableObjectToString(fixtures.TeamWon),
                     FirstBattingTeam = NullableObjectToString(fixtures.FirstBattingTeam) // ..... you get the idea
                  }
answered 2012-07-05T06:50:05.303

Your Answer