Search code examples
asp.net-mvcasp.net-mvc-3asp.net-mvc-4asp.net-mvc-viewsasp.net-mvc-viewmodel

How can you do multiple saves of a model in one submit?


I have a model:

public class CustomerAttributes
{
    public Int Id { get; set; }
    public string value { get; set; }
}

my create view looks like this:

    <div class="editor-label">
        @Html.Label("Name")
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Value)
        @Html.ValidationMessageFor(model => model.Value)
    </div>

    <div class="editor-label">
        @Html.Label("Age")
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Value)
        @Html.ValidationMessageFor(model => model.Value)
    </div>

    <div class="editor-label">
        @Html.Label("Height")
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Value)
        @Html.ValidationMessageFor(model => model.Value)
    </div>

So each textbox will be a new record in the database. Is it possible to do something like this? How can I handle this? Also my biggest problem is that each field could be a textbox or a combobox or a radio.... etc... Name might a textbox for example but age might be a combobox.... I know I have all textboxes now but that could change.


Solution

  • The link provided in a comment was helpful:

    http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

    To add to that, there could be a ViewModel that can hold a string or an enum or something to specify what type of control to display and with that some partial views that display the different controls. Then iterate through and display each control in the view based on that specifier.