You could return the joined array to a view model class (untested for syntax/typos):
public class ViewModel
{
public KeyValuePair<string,string> Fields { get; set; }
}
var kvps = Fields.Join(FieldValues,
f => f.FieldId,
fv => fv.FieldId,
(f, fv) =>
new KeyValuePair<string,object>(f.Value, fv.Value)
}).ToArray();
var viewModel = new ViewModel { Fields = kvps };
return View(viewModel);
Then in the view, simply iterate over the values:
@model MyNamespace.ViewModel
@foreach (var kvp in Model.Fields)
{
<span class="field-label">@kvp.Key</span>
<span class="field-value">@Html.TextBoxFor(_ => kvp.Value)</span>
}
If you put this in a form, then the postback should also work correctly (since it uses TextBoxFor
).