입니다. foreach 루프가 포함 된보기에 뷰 모델을 전달하고 있습니다. 이보기 모델에는 두 개의 목록이 있습니다. 이 목록은 Linq를 사용하여 채워진다.MVC3 사전에 전달 된 모델 항목은 '_320.Models.MyViewModel'유형이지만
다음 코드를 사용하면 다음 오류가 발생합니다. 사전에 전달 된 모델 항목의 형식이 '_320.Models.MyViewModel'이지만이 사전에는 'System.Collections.Generic.IEnumerable'유형의 모델 항목이 필요합니다. `1 [_320.Models.MyViewModel] '.
내 뷰의 foreach 루프에서 ViewModel의 두 목록에 포함 된 속성에 도달하려고합니다.
내가 내 컨트롤러 코드의 끝 부분을 수정하여보기를 표시 할 수 있었다 업데이트 :
List<MyViewModel> MyList = new List<MyViewModel>();
MyList.Add(_myViewModel);
return View(MyList);
그러나, 헤더 아무것도하지만, 빈이다. 내보기의 필드를 @Html.DisplayFor(modelItem => item.rx.Rx_ID)
에서 @Html.DisplayFor (modelItem => item.Rxes.rx.Rx_ID)
으로 변경하려고하면 빨간색으로 표시됩니다. 멀리 떨어진 곳에 위치한 부동산에 접근하는 것이 불가능합니까? UPDATE의
END
모델
public class MyViewModel
{
public Patient patient { get; set; }
public Rx rx { get; set; }
public Fill fill { get; set; }
public List<Rx> Rxes { get; set; }
public List<Fill> Fills { get; set; }
컨트롤러
public ActionResult AllFilled()
{
MyViewModel _myViewModel = new MyViewModel();
List<Fill> FillList = db.Fills.Where(p => p.Status == "Filled").ToList();
List<Rx> RxList = new List<Rx>();
Rx myRx = new Rx();
foreach (var item in FillList)
{
myRx = db.Rxes.Single(p => p.Rx_ID == item.Rx_ID);
RxList.Add(myRx);
}
_myViewModel.Fills = FillList;
_myViewModel.Rxes = RxList;
return View(_myViewModel);
}
}
보기
@model IEnumerable<_320.Models.MyViewModel>
@{
ViewBag.Title = "AllFilled";
}
<h2>AllFilled</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
NDC
</th>
<th>
Rx_ID
</th>
<th>
Fill_ID
</th>
<th>
Status
</th>
<th>
Filled Date
</th>
<th>
Filled By
</th>
<th></th>
</tr>
{
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.rx.NDC)
</td>
<td>
@Html.DisplayFor(modelItem => item.rx.Rx_ID)
</td>
<td>
@Html.DisplayFor(modelItem =>item.fill.Fill_ID)
</td>
<td>
@Html.DisplayFor(modelItem => item.fill.Status)
</td>
<td>
@Html.DisplayFor(modelItem => item.fill.Filled_Date)
</td>
<td>
@Html.DisplayFor(modelItem => item.fill.Filled_By)
</td>
<td>
@Html.ActionLink("Details", "Details", new { id=item.rx.Rx_ID })
</td>
</tr>
}
</table>
이 줄을 변경 GetEnumerator – HendPro12
@foreach (모델의 var 항목) 행에 따르면 모델은 루프에서 사용할 때 fill 및 rx 객체를 포함하는 MyViewModel 객체의 목록 일뿐입니다. 그러나 하나의 MyViewModel 개체에 컨트롤러의 fill 및 rx 개체 목록을 두 가지로 채 웁니다. 대신 MyViewModel List를 작성해야하며, 각 List에는 fill 및 rx 객체가 있어야합니다. – Prafulla