Question

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.

Was it helpful?

Solution 2

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.

OTHER TIPS

I would personally suggest creating a IEnumerable ViewModel of your CustomerAttributes model.

This way you could pass multiple values back to the controller.

Each time you added the new form elements (assuming you are adding them dynamically), you would just create a new item within the model.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top