2013-08-01 2 views
0

내가 추가 한 경우 IF MVC3보기에서 조건 있지만 작동하지 : Object reference not set to an instance of an object.MVC3보기의 조건은

조건을 확인하는 경우의 목적 : IF 문 실행하는 동안

@model IEnumerable<StudentRegistrationPortal.Models.CourseRegisterModel> 
@{ 
    ViewBag.Title = "Welcome Student"; 
} 

<h2>Welcome 
@Context.User.Identity.Name 
</h2> 
@Html.ActionLink("[Sign Out]", "SignOut", "Student") 
<ul> 
    <li>@Html.ActionLink("Register Courses", "registerCourses", "Course")</li> 
</ul> 

<h3>Pending Courses</h3> 
<table border="1"> 
<tr> 
    <th>RollNumber 
    </th> 
    <th>Course Code 
    </th> 
    <th>Course Name 
    </th> 
    <th>Status</th> 
</tr> 

@foreach (StudentRegistrationPortal.Models.CourseRegisterModel modelValue in Model) 
{ 
    if (!string.IsNullOrEmpty(modelValue.Course.Code)) 
    { 
    <tr> 
     <td> 
      @Context.User.Identity.Name 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => modelValue.Course.Code) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => modelValue.Course.Name) 
     </td> 
     <td>Pending 
     </td> 
    </tr> 
    } 
} 

</table> 

그것은 오류를 다음 부여합니다 그 modelValue.Course.Code 값은 빈 문자열 또는 null이 아닙니다.

답변

5

Course 자체가 null 일 가능성이 큽니다. 두 가지를 모두 확인해야합니다.

if (modelValue.Course!=null && !string.IsNullOrEmpty(modelValue.Course.Code)) 
{ 
//etc 
} 
+0

resharper 101 : D – phillip