업데이트 : 당신은 IGrouping에 대한 자신의 EditorTemplate를 작성해야합니다. IGrouping에 대한 EditorTemplate의
샘플 : 모델 :
public class Row
{
public Row()
{
}
public Row(string key, int value)
{
Key = key;
Value = value;
}
public int Value { get; set; }
public string Key { get; set; }
}
컨트롤러 :
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
IEnumerable<Row> dict = new[] { new Row("1", 1), new Row("1", 2), new Row("1", 3), new Row("2", 2), new Row("2", 3), new Row("2", 4) };
var grouped = dict.GroupBy(c => c.Key).First();
//var grouplist = grouped.Select(c => new KeyValuePair<string, IEnumerable<int>> (c.Key, c.Select(b=>b.Value)));
return View(grouped);
}
public ActionResult Post(IEnumerable<Row> Model)
{
return null;
}
}
보기 색인 :
@model IGrouping<string, MvcApplication1.Models.Row>
@{
ViewBag.Title = "Home Page";
}
@using(Html.BeginForm("Post", "Home", FormMethod.Post))
{
@Html.EditorForModel("IGrouping")
<button type="submit">Submit</button>
}
EditorTemplate IGrouping.cshtml : 당신이 읽을 수있는 바인딩 컬렉션에 대한
@model IGrouping<string, MvcApplication1.Models.Row>
@{
@Html.DisplayFor(m => m.Key)
int i = 0;
foreach (var pair in Model.Select(c => c))
{
@Html.Hidden(string.Format("Model[{0}].Key", i), pair.Key)
@Html.TextBox(string.Format("Model[{0}].Value", i), pair.Value)
i++;
}
}
더 : Hancelman's blog
컴파일되지 않음 – Kelly
"컬렉션"템플릿의 내용을 표시 할 수 있습니까? –
실제로 템플릿에 내장되어 있으므로 "Collection"매개 변수의 유무와 상관없이 작동해야합니다. – Kelly