null 참조에 대해 런타임 바인딩을 수행할 수 없지만 null 참조는 아닙니다.
사용: MVC 4, ASP.NET 레이저
불가능할 것 같은 오류가 발생하고 있습니다.주(州)라는 널 참조를 사용하고 있다고 하지만 분명히 설정되어 있습니다.
컨트롤러:
public ActionResult Index()
{
Dictionary<int, string> states = new Dictionary<int, string>()
{
{ -1, "a"},
{ 0, "b"},
{ 1, "c"},
{ 2, "d"},
};
//assigning states
ViewBag.States = states;
foreach (KeyValuePair<int, string> de in ViewBag.States)
{
Debug.WriteLine(de.Key);
}
return View();
}
보기:
<div class="search-input">
<select>
@foreach (KeyValuePair<int, string> de in ViewBag.States)
{
<option value="@de.Key">@de.Value</option>
}
</select>
</div>
오류:
Cannot perform runtime binding on a null reference
Line 54: @foreach (KeyValuePair<int, string> de in ViewBag.States)
찾은 솔루션:제가 보기엔 오타가 있었어요, 뷰백Typo <--- 오류가 발생했지만 디버거는 관련 없는 위치에 예외를 배치했습니다.
이 오류는 레이저 코드에서 메서드를 호출하는 ViewBag가 존재하지 않는 경우에 발생합니다.
컨트롤러
public ActionResult Accept(int id)
{
return View();
}
면도기:
<div class="form-group">
@Html.LabelFor(model => model.ToId, "To", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.Flag(Model.from)
</div>
</div>
<div class="form-group">
<div class="col-md-10">
<input value="@ViewBag.MaximounAmount.ToString()" />@* HERE is the error *@
</div>
</div>
어떤 이유에서인지 .net이 올바른 행에 오류를 표시할 수 없습니다.
일반적으로 이것은 많은 시간 낭비를 야기합니다.
이 예외는 존재하지 않는 속성이 반사를 사용하여 동적으로 업데이트되는 경우에도 발생합니다.
반사를 사용하여 동적으로 속성 값을 업데이트하는 경우 합격 여부를 확인할 필요가 있습니다.PropertyName실제 속성과 동일합니다.
저의 경우, 저는 업데이트를 시도하고 있었습니다.Employee.firstName하지만 그 재산은 사실.Employee.FirstName.
명심할 가치가 있습니다.:)
이 오류에 대한 나의 해결책은 다음과 같은 참조가 있는 다른 프로젝트의 복사 및 붙여넣기입니다.@Model.Id이 특정 페이지에는 모델이 없었지만 오류 라인이 실제 오류와 너무 멀리 떨어져 있어서 찾을 수 없었습니다!
null과 같지 않은 상태를 정의해야 합니다.
@if (ViewBag.States!= null)
{
@foreach (KeyValuePair<int, string> de in ViewBag.States)
{
value="@de.Key">@de.Value
}
}
레이저와 관련이 없습니다. 동적 값 내에 존재하지 않는 속성에 액세스를 시도하는 런타임에 예외가 표시됩니다.
dynamic myValue;
if(myValue.notExistedProperty == "some-value") { ... }
세트
Dictionary<int, string> states = new Dictionary<int, string>()
기능 외부의 속성으로, 그리고 기능 내부에 항목을 삽입하면 작동해야 합니다.
언급URL : https://stackoverflow.com/questions/19457804/cannot-perform-runtime-binding-on-a-null-reference-but-it-is-not-a-null-referen
'programing' 카테고리의 다른 글
| Vue.js / Vuex + axios가 여러 PUT 요청을 전송함 (0) | 2023.07.02 |
|---|---|
| 0으로 한 자리 숫자 채우기 (0) | 2023.07.02 |
| asp.net 에서 global.asax의 목적은 무엇입니까? (0) | 2023.07.02 |
| VBA(Excel)에서 표준 시간대 정보 가져오기 (0) | 2023.07.02 |
| mongodb에서 문서를 캐스케이드로 삭제하는 방법은 무엇입니까? (0) | 2023.06.27 |