MVC의 장점은 여러 가지 다른 방법으로 해결할 수 있다는 것입니다. 특정 시나리오에 가장 적합한 솔루션을 찾아야합니다. 숨겨진 필드가 확실히 작동하지만, 항상 가장 이상적인 솔루션은 아닙니다.
컨트롤러
public class ExampleController
{
static Dictionary<int, string> sampleViewModel = new Dictionary<int, string>
{
{1, "Example Item 1"},
{2, "Example Item 2"},
{3, "Example Item 3"},
};
public ActionResult Index()
{
return View(sampleViewModel);
}
[HttpPost]
public ActionResult Delete(int id)
{
sampleViewModel.Remove(id);
return RedirectToAction("Index");
}
}
보기
@model System.Collections.Generic.Dictionary<int, string>
<html>
<head>
...
</head>
<body>
<table>
<thead>
<tr>
<th>Item</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>@item.Key</td>
<td>
@using (Html.BeginForm("Delete", new { id = item.Value })) {
<input type="submit" value="Delete" />
}
</td>
</tr>
}
</tbody>
</table>
</body>
</html>
난 그냥 어떤 구문 오류에 대한 내 사과 있도록 함께 신속하게 던졌다 : 여기에 빠른 예제 시나리오를 삭제합니다. 희망이 도움이!
감사합니다. 내가 할 수 있다면 또 하나의 질문입니다. 수정을 시도 할 경우 업데이트 된 모델 데이터에 액세스하는 방법이 있습니까? 나는 이것을위한 예제를 찾는 시간을 가지고있다. –
물론입니다. 다음 중 하나를 수행 할 수 있습니다. @using (Html.BeginForm()) { –
나는 그것을 시도하고있다. 그러나 제출 된 모델 데이터에 액세스하는 방법은 여전히 확실하지 않습니다. –