컨트롤러로 돌아 가기 위해 필요한 여러 행의 데이터가있는 테이블이 있습니다. 필자는 처음에는 시간대를 선택하고 버튼을 클릭하여 표를로드합니다. 테이블은 모든 관련 레코드를로드하지만 내 테이블 셀 중 하나에는 드롭 다운 목록이 있습니다. 따라서 드롭 다운에서 "업데이트"를 클릭하면 내 컨트롤러가 변경 사항을 저장할 수 있습니다.MVC : 컨트롤러에 여러 행의 데이터 반환
내가 시도하고 저장할 때까지 모든 것이 작동합니다. 컨트롤러에 전송 된 모델은 완전히 null입니다. 테이블 셀에 연결된 목록 속성은 컨트롤러 null로 반환됩니다.
@ModelType SuperViewModel
//We need this a view model in order to store a List of the models in the table
@Using (Html.BeginForm())
@For Each i in Model.CompleteList
Dim currentItem = i //MVC auto-generated extra declarations. Seems redundant to me but it works.
@<table>
<tr>
<td>@Html.DisplayFor(function(Model)currentItem.Name)</td>
<td>@Html.DisplayFor(function(Model)currentItem.SampleTime)</td>
<td>@Html.DropDownListFor(function(Model)currentItem.WorkTime, ViewBag.WorkTimeList)</td>
</tr>
Next
</table>
<input name="submit" type="submit" value="Update"/>
End Using
//Controller
<HttpPost()>
function Save(vmodel as SuperViewModel, submit as String) as ActionResult //NOTE: submit parameter is used because we have two submit buttons but its not relevant here
if submit = "Update"
db.Entry(vmodel.CompleteList).State = EntityState.Modified//Here the exception is throw because our list is null at this point even tho its tied to the model in the view.
db.SaveChanges()
end if
End Function
참고 : 이것은 VB.NET로 작성되었지만 C# 도움말을 환영합니다. MVC에서 두 언어에 익숙합니다.
SuperViewModel은 어떻게 정의됩니까? –
여기에는 여러 속성이 있지만 여기에 적용되는 속성은 List (Of AnotherModel) 인 "CompleteList"입니다. Entity Framework에서 DB 테이블을 기반으로 생성 한 다른 모델 – ExceptionLimeCat
'끝내기 '전에'다음'이 누락되어'For Each'를 닫습니까? 또한보기에서 렌더링 한 마크 업을 추가 할 수 있습니까? –