0

Models

public class IntegerList
{
    public int IntegerListID { get; set; }
    public string Direction { get; set; }
    public long Performance { get; set; }
    public virtual ICollection<Integer> Integers { get; set; }
}

public class Integer
{
    public int IntegerID { get; set; }
    [Required(ErrorMessage = "An integer is Required")]
    [Range(0, 9999999, ErrorMessage = "Enter an integer")]
    public int IntegerValue { get; set; }
    public int IntegerListID { get; set; }
    public virtual IntegerList IntegerList { get; set; }
}

The above models are for an application that sorts a range of integers into ascending or descending order and calculates the time taken to perform the sort.

The integers are entered into textboxes by the user who can add or remove them using jquery.

I've got the application working by passing formcollection to the controller, splitting the string of integers into an array of integer values to be added to IntegerList.Integers.

However, I'm looking for a more elegant strongly-typed solution.

ViewModel

public class IntegerViewModel
{
    [UIHint("Integers")]
    public IEnumerable<Integer> Integers { get; set; }
    public string Direction { get; set; }
}

Where I'm struggling is adding Integers.IntegerValues to the view so they can be passed to the controller via the viewmodel. I also need to increment/decrement each IntegerValue textbox value as it is added/removed by jquery. I'm not sure I'm approaching this from the right angle.

I hope that's clear. Thanks for your assistance.

Razor

    <form method="post">
        <div id="integers">
            @Html.Label("Enter Integers")
            <div class="integer">
                @Html.EditorFor(model => model.Integers)
                <a href="#" id="addInt">Add Integer</a>
            </div>
        </div>
        @Html.Label("Sort Direction")
        <div>
            @Html.DropDownListFor(model => model.Direction, new[] {
                new SelectListItem() {Text = "Ascending", Value = "Ascending"},
                new SelectListItem() {Text = "Descending", Value = "Descending"}
            })

            <input class="button" type="submit" value="Submit" />
        </div>
    </form>

Editor Template (for Integers)

@model Integer

@Html.EditorFor(m => m.IntegerValue)
4

1 に答える 1