开发者

how to set exist value as selected to dropdown in asp.net mvc dropdown

I have a form in asp.net mvc that retrieves records from the database. this form have dropdowns. in Edit action of controller it is must to have selected the already inserted items. suppose if i am taking the User information开发者_如何学JAVA for edit, then its City,State and country must be selected as they inserted before.

So i tried in Edit:

 int CityId_ = objAM.GetCityName(User.CityId);
                        ViewData["Cites"] = new SelectList(City.GetAllCities(), "CityId", CityName", CityId_);

same like country and state. but though CityId_ have value but it no appear like that. It defaults from first city (same for all) why this because ?

i just gone throught this blog where i found , the assigning third parameter name should be in the object's property. but let say I have primary and Fk relationship, but columns name are different then it seems MVC strongly coupled feature fail. Why this is ?


I experienced the same issue and this is what I did to get my drop down list to select correct value in the drop down list when editing a record.

Team team = _db.TeamSet.First(m => m.TeamID == id);
ViewData["FantasyYear"] = new SelectList(fanYear, "YearID", "FantasyYear", team.FanYearReference.EntityKey.EntityKeyValues[0].Value);

This was the only way I saw this could be done. You must pass the refernced table's entitykey and key values. Since I'm brand new at MVC, I cannot break down this line of code too well. fanYear is the IEnumerable item. YearID is the value (key column in the referenced table) of the drop down list, "FantasyYear" is the display text of the drop down list. team is the table that I'm editing. I believe I'm setting up a reference to FanYear table and getting the team's fantasy year key. It's telling it the year's key value. I'm not sure why [0] is needed.

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜