在 ASP.NET MVC 3 应用程序中填充下拉框时出现问题
我已经在 www 上完成了 MVC 3 的新教程(musicstore) .asp.net。一切都很顺利,除了应该从数据库填充两个下拉框的部分 - 但事实并非如此。
我按照教程进行操作并仔细检查了我的代码。我认为问题可能出在使用 editorstemplate 文件夹。自从我刚接触 MVC 以来,真的不知道。那么问题是什么或者我该如何调试它?
==============
编辑 1
好吧,这里是 album.cshtml 的一些代码,它位于 /views/shared/editortemplates/ 文件夹中
@model MvcMusicStore.Models.Album
<p> @Html.LabelFor(model => model.Genre) @Html.DropDownList("GenreId",
new SelectList(ViewBag.Genres as System.Collections.IEnumerable,
"GenreId", "Name", Model.GenreId))
</p>
<p> @Html.LabelFor(model => model.Artist) @Html.DropDownList("ArtistId",
new SelectList(ViewBag.Artists as System.Collections.IEnumerable,
"ArtistId", "Name", Model.ArtistId))
</p>
,我相信它是从以下位置填充的:
public ActionResult Edit(int id)
{ ViewBag.Genres = storeDB.Genres.OrderBy(g => g.Name).ToList(); ViewBag.Artists = storeDB.Artists.OrderBy(a => a.Name).ToList();
var album = storeDB.Albums.Single(a => a.AlbumId == id);
return View(album);
}
我不'除了下拉菜单未填充之外,不会出现任何错误...
==============
编辑 2
所以我在 /views/storemanager/edit.cshtml 中有 edit.cshtml ,然后我在 /views/shared/editortemplates/album.cshtml 中有 album.cshtml 。下拉列表应该从 album.cshtml 填充到 edit.cshtml 中。我将 album.cshtml 中的代码直接放入 edit.cshtml 中,效果很好。所以我认为问题是 editortemplates/album.cshtml 不起作用,即填充 edit.cshtml 页面。那么什么给出呢?谢谢...
==============
编辑 3
好的,我发现了问题,我从 CodePlex 获得了工作源。看来我没有正确设置 create.cshtml 和 edit.cshtml 页面。 无论如何,现在一切都已修复,谢谢...
I have completed the new tutorial (musicstore) on MVC 3 over on www.asp.net. It all went fine except for the part where two dropdown boxes should be populated from the database - and they are not.
I followed the tutorial and double checked my code. I think the problem may be using the editorstemplate folder. No idea really since Im new to MVC. So whats the problem or how can I debug it?
==============
Edit 1
okay so here is some of the code for album.cshtml which is in the /views/shared/editortemplates/ folder
@model MvcMusicStore.Models.Album
<p> @Html.LabelFor(model => model.Genre) @Html.DropDownList("GenreId",
new SelectList(ViewBag.Genres as System.Collections.IEnumerable,
"GenreId", "Name", Model.GenreId))
</p>
<p> @Html.LabelFor(model => model.Artist) @Html.DropDownList("ArtistId",
new SelectList(ViewBag.Artists as System.Collections.IEnumerable,
"ArtistId", "Name", Model.ArtistId))
</p>
which I believe is populated from:
public ActionResult Edit(int id)
{ ViewBag.Genres = storeDB.Genres.OrderBy(g => g.Name).ToList(); ViewBag.Artists = storeDB.Artists.OrderBy(a => a.Name).ToList();
var album = storeDB.Albums.Single(a => a.AlbumId == id);
return View(album);
}
I don't get any errors apart from the fact the dropdowns are not populated...
==============
Edit 2
so I have edit.cshtml in the /views/storemanager/edit.cshtml and then I have album.cshtml in /views/shared/editortemplates/album.cshtml. The dropdowns are supposed to be populated from album.cshtml into edit.cshtml. I put the code from album.cshtml directly into edit.cshtml and it works fine. So I think the problem is that the editortemplates/album.cshtml is not working i.e. populating the edit.cshtml page. So what gives? Thanks...
==============
Edit 3
Ok I found the problem, I got the working source from CodePlex. It seems I didnt have the create.cshtml and edit.cshtml pages setup properly.
Anyway all fixed now so thanks...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您使用视图模型并避免使用任何
ViewBag
。因此,您首先定义一个视图模型:然后在控制器操作中填充此视图模型:
最后在编辑器模板中(
~/Views/Shared/EditorTemplates/AlbumViewModel.cshtml
):I would recommend you working with view models and avoid using any
ViewBag
. So you start by defining a view model:and then inside your controller action you would populate this view model:
and finally in your editor template (
~/Views/Shared/EditorTemplates/AlbumViewModel.cshtml
):好的,我找到了问题,我从 CodePlex 获得了工作源。看来我没有正确设置 create.cshtml 和 edit.cshtml 页面。无论如何,现在一切都已修复,谢谢...
Ok I found the problem, I got the working source from CodePlex. It seems I didnt have the create.cshtml and edit.cshtml pages setup properly. Anyway all fixed now so thanks...