我有一個問題,當從視圖到控制器的對象中傳遞一個列表時,列表始終爲空。我應該怎麼做?如何從視圖傳遞列表到控制器
我的模型:
public class ImageModel
{
public int id { get; set; }
public string url { get; set; }
public bool delete { get; set; }
}
public class SchoolImages
{
public Nullable<int> schoolid { get; set; }
public IList<ImageModel> images;
}
的觀點:
@model SchoolAppManager.Models.SchoolImages
@using (Html.BeginForm("DeleteImages", "Images"))
{
@Html.HiddenFor(x => x.schoolid)
<table>
@for (int i = 0; i < Model.images.Count(); i += 4) {
<tr>
<td>
<img src="@Url.Content(Model.images[i].url)" width="50%" />
<br />
@Html.CheckBoxFor(x => x.images[i].delete)
@Html.HiddenFor(x => x.images[i].id)
</td>
<td>
@if (i + 1 < Model.images.Count())
{
<img src="@Url.Content(Model.images[i + 1].url)" width="50%" />
<br />
@Html.CheckBoxFor(x => x.images[i + 1].delete)
@Html.HiddenFor(x => x.images[i + 1].id)
}
</td>
<td>
@if (i + 2 < Model.images.Count())
{
<img src="@Url.Content(Model.images[i + 2].url)" width="50%" />
<br />
@Html.CheckBoxFor(x => x.images[i + 2].delete)
@Html.HiddenFor(x => x.images[i + 2].id)
}
</td>
<td>
@if (i + 3 < Model.images.Count())
{
<img src="@Url.Content(Model.images[i + 3].url)" width="50%" />
<br />
@Html.CheckBoxFor(x => x.images[i + 3].delete)
@Html.HiddenFor(x => x.images[i + 3].id)
}
</td>
</tr>
}
</table>
<input type="submit" value="delete" />
}
當我點擊刪除,SchoolImages
傳遞給控制器,SchoolImages.schoolId
在控制器值,然而,SchoolImages.images
爲空。我怎麼能通過SchoolImages.images
控制器?
我知道將一個列表傳遞給控制器的唯一方法是使用jquery構建它並通過ajax調用傳遞它。如果可能的話,傳遞一個id並重新創建控制器列表 –