如何将现有值设置为在asp.net mvc下拉列表中选择的下拉列表

发布于 2024-10-08 22:05:57 字数 620 浏览 0 评论 0原文

我在 asp.net mvc 中有一个表单,可以从数据库中检索记录。该表格有下拉菜单。在控制器的编辑操作中,必须选择已插入的项目。假设如果我要编辑用户信息,则必须选择之前插入的城市、州和国家/地区。

所以我尝试编辑:

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

与国家和州相同。但虽然 CityId_ 有值,但它看起来并不像那样。它默认来自第一个城市(所有城市都相同)为什么这是因为?

我刚刚经历过这个 blog 我发现,分配第三个参数名称应该在对象的属性中。但假设我有 Primary 和 Fk 关系,但列名称不同,那么 MVC 强耦合功能似乎失败了。这是为什么?

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 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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给不了的爱 2024-10-15 22:05:57

我遇到了同样的问题,这就是我在编辑记录时让下拉列表在下拉列表中选择正确的值所做的。

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

这是我认为可以做到的唯一方法。您必须传递引用表的实体键和键值。由于我是 MVC 的新手,所以我无法很好地分解这行代码。 fanYear 是 IEnumerable 项。 YearID 是下拉列表的值(引用表中的关键列),“FantasyYear”是下拉列表的显示文本。 team 是我正在编辑的表。我相信我正在设置对 FanYear 表的引用并获取团队的幻想年密钥。它告诉它今年的关键值。我不确定为什么需要 [0]。

希望这有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文