0

I have a SelectList in my MVC2 model.

In the case that there's only 1 item in the SelectList I want this one item automatically selected (in my view I add an additional item).

My problem is that I can't get this single item to be selected.

So the class behind my model has

if (Clients.Count() == 1)
{
     Clients.First().Selected = true;
}

But immediately after stepping through this line, if I add a watch to Clients I can see that Selected = null.

In addition, on my View I have

<%:Html.DropDownListFor(c => c.Client, Model.Clients, "-- Select Client --") %>

When the page loads -- Select Client -- is always selected.

Can anyone explain how I can get the model to correctly mark the item as being selected?

4

1 に答える 1

0

ビュー モデルにはプロパティ Model.Client が必要であり、(上記のコードから) その値を設定していないように見えますか?

おそらくコントローラーアクションで:

Model.Client = Clients.First();

ただし、設定する必要があるのは Model.Client プロパティであり、SelectList オプションではありません。

于 2011-04-23T09:10:27.317 に答える