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
456 views
in Technique[技术] by (71.8m points)

asp.net mvc 4 - SelectListItem in Selected Property doesnt work

Controller

Kullanicilar kullanici = db.Kullanicilar.Where(f => f.Id == 
   userProfileModel.UserModel.Kullanici.UserID).FirstOrDefault();

foreach (var item in db.Tanimlar.Where(f => f.TanimTipId == (int)ETanimTip.Kategoriler))
userProfileModel.AvaliableCategories.Add(item: new SelectListItem() 
{ 
    Text = item.Kod, 
    Value = item.Id.ToString(), 
    Selected = item.Id == kullanici.CategoryId 
});

View

@Html.DropDownListFor(f => f.CategoryId, Model.AvaliableCategories, 
  htmlAttributes: new { @class = "form-control", id = "AvaliableCategories"  })

When i was a look at selected value, i see true but view result is false.. true value false result What do i now?


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

1 Answer

0 votes
by (71.8m points)
@foreach (var item in Model.AvaliableCategories)
                {
                    if (item.Selected)
                    {
                        <option value="@item.Value" selected="selected">@item.Text</option>
                    }
                    else
                    {
                        <option value="@item.Value">@item.Text</option>
                    }
                }

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

...