My first thought was to use a Dictionary
, but that's not indexable, so you'd have to write a custom model binder. Not that hard, but still. Then I thought about using a List>
, but KeyValuePair
s have private setters, so, again, you'd need a custom binder.
So I think the best way is:
Создание настраиваемого типа
public class MyItems
{
public string Key { get; set; }
public string Value { get; set; }
}
Теперь добавьте список этого типа в качестве свойства в вашу модель просмотра
public List MyItems { get; set; }
И, заполнив список и строго набрав ваше мнение, вы можете отобразить таблицу с помощью встроенных html-помощников, чтобы гарантировать, что привязка к модели не будет иметь никаких икота
@for (int i = 0; i < Model.MyItems.Count( ); i++ )
{
<tr>
<td>@Html.TextBoxFor( m => m.MyItems[i].Key )</td>
<td>@Html.TextBoxFor( m => m.MyItems[i].Value)</td>
</tr>
}
Затем поймайте модель в контроллере и получите доступ к данным
[HttpPost]
public ActionResult Index(Model viewModel)
{
foreach (var item in viewModel.MyItems)
{
string columnOneValue = viewModel.MyItems[0].Key;
string columnTwoValue = viewModel.MyItems[0].Value;
}