I have a table called Field in my database and a table called FieldValue. My models look like this:
[Table("Field")]
public class Field
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int FieldId { get; set; }
public string Value { get; set; }
}
[Table("FieldValue")]
public class FieldValue
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int FieldValueId { get; set; }
public int FieldId { get; set; }
public string Value { get; set; }
}
I would like to display each field as the label and then a textbox for each FieldValue associated with that Field but I’m not quite sure how to approach this. I am using MVC 4 if that matters.
You could return the joined array to a view model class (untested for syntax/typos):
Then in the view, simply iterate over the values:
If you put this in a form, then the postback should also work correctly (since it uses
TextBoxFor).