Thomas Stringer
Thomas Stringer

Reputation: 5862

DropDownListFor Value Field - Property Combination Identifer

I'm current wrestling with how to determine selected data from a HtmlHelper.DropDownListFor object. The problem is that there is no single property of the model class that would be able to uniquely identify the model.

Below is my model and viewmodel classes, as well as a sample of the HtmlHelper.DropDownListFor call in the view.

public class MyModel
{
    public int ParentId { get; set; }   // combined with ChildId is the unique identifier
    public int ChildId { get; set; }    // see above
    public string Name { get; set; }    // not unique
}

public class MyViewModel
{
    public IEnumerable<MyModel> MyModels { get; set; }

    public <???> SelectedItem { get; set; }
}

@Html.DropDownListFor(m => m.SelectedItem, new SelectList(Model.MyModels, "DataValueField", "Name"))

The problem here is that the only unique idenfier of MyModel is the combination of ParentId and ChildId. So what would be the best way to retrieve the data to uniquely identify the selected item?

To construct a unique identifier from one of these model objects would be similiar to the following (business logic):

ModelUniqueIdentifier = ParentId * 100 + ChildId

But I don't see any way to enforce this logic to set MyViewModel.SelectedItem to this.

When there is a postback from whatever triggers the form to submit, I'd like to be able to grab the selected value from the model's SelectedItem property in order to do other processing. But with a one-dimensional "DataValueField", I'm not sure the best way to accomplish this.

The most obvious way to circumvent this would be to inject a surrogate key into the data as a singleton identifier. But with this particular data, there is just this obvious composite natural key that has been doing a great job up until this point.

So the two-dimensional data looks like:

ParentId    ChildId Name
7           3       Name1
4           8       Name2
5           3       Name3
4           6       Name4

If I was to add a surrogate key to the data it would do the job:

SurrogateId ParentId    ChildId Name
0           7           3       Name1
1           4           8       Name2
2           5           3       Name3
3           4           6       Name4

Whereas this would work, it would require me to change the underlying table, model, and a large amount of referencing code, so if I don't have to do add this surrogate key then I'd rather not.

Any thoughts or ideas? Thank you in advance!

Upvotes: 0

Views: 95

Answers (1)

Gareth Hopkins
Gareth Hopkins

Reputation: 354

You could try using a calculated get property in your model that generates your unique ID on calling. Then pass 'UniqueId' as the data value field for the select list / dropdown. E.g.

public class MyModel
{
    public int UniqueId { 
        get {
            return ParentId * 100 + ChildId;
        }
    }
    public int ParentId { get; set; }   // combined with ChildId is the unique identifier
    public int ChildId { get; set; }    // see above
    public string Name { get; set; }    // not unique
}

SelectItem would then be an int in this case.

Upvotes: 1

Related Questions