Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

drop down menu - Populating ASP.NET MVC DropDownList

OK, I've been Googling for hours and trying everything and can't get anything to work. I am learning MVC using Sharp Architecture and have generated some basic forms for creating Client objects. I want to fill the state drop down list with a list of US states and let the user pick from that list. I am able to populate the list and get the value back (to save the client) but when I go to edit the client, the client's current state is not selected. I have set the selected value in the SelectList:

<li>
    <label for="Client_StateProvince">StateProvince:</label>
    <div>
        <%= Html.DropDownListFor(c=>c.Client.StateProvince, new SelectList(Model.StateProvinces, "id", "Name", Model.Client.StateProvince), "-- Select State --")%>
    </div>
    <%= Html.ValidationMessage("Client.StateProvince")%>
</li>

This does not seem to be good enough. What am I missing?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
<%= Html.DropDownListFor(c => c.Client.StateProvince.Id, 
                              new SelectList(Model.StateProvinces, 
                                             "id", 
                                             "Name", 
                                             Model.Client.StateProvince), 
                              "-- Select State --")%>

This does it.

Hope this helps someone else.

~Lee


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...