문제

I have used this snippet in my view

<SELECT  name="id_category">
             @for (int item = 0; item < list_cat.Count; item++){
                    if (@list_cat[item].Id_category == @Model.Id_category) {
                    <OPTION VALUE="@list_cat[item].Id_category" selected >@list_cat[item].CName</OPTION>}
                    else{
                    <OPTION VALUE="@list_cat[item].Id_category" >@list_cat[item].CName</OPTION>
                        }
                                               }
                  </SELECT>

then, i replaced it by

 @Html.DropDownList("id_category", new SelectList(list_cat,"Id_category","CName"))

But the condition of selection of the item is missing.

How can i replace it with Html.DropDownList? Any suggestions?

도움이 되었습니까?

해결책

Change your DropDownList like below:

@Html.DropDownList("id_category", new SelectList(list_cat, "Id_category", "CName", Model.Id_category))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top