0

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?

1 Answer 1

2

Change your DropDownList like below:

@Html.DropDownList("id_category", new SelectList(list_cat, "Id_category", "CName", Model.Id_category))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.