在 ASP.NET MVC 3 应用程序中填充下拉框时出现问题

发布于 2024-10-15 03:23:08 字数 1633 浏览 2 评论 0原文

我已经在 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 技术交流群。

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

发布评论

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

评论(2

巡山小妖精 2024-10-22 03:23:08

我建议您使用视图模型并避免使用任何 ViewBag。因此,您首先定义一个视图模型:

public class AlbumViewModel
{
    public string GenreId { get; set; }
    public IEnumerable<Genre> Genres { get; set; }

    public string ArtistId { get; set; }
    public IEnumerable<Artist> Artists { get; set; }

    public Album Album { get; set; }
}

然后在控制器操作中填充此视图模型:

public ActionResult Edit(int id)
{
    var model = new AlbumViewModel
    {
        Genres = storeDB.Genres.OrderBy(g => g.Name),
        Artists = storeDB.Artists.OrderBy(a => a.Name),
        Album = storeDB.Albums.Single(a => a.AlbumId == id)
    };
    return View(model);
}

最后在编辑器模板中(~/Views/Shared/EditorTemplates/AlbumViewModel.cshtml):

@model MvcMusicStore.Models.AlbumViewModel
<p> 
    @Html.LabelFor(model => model.GenreId) 
    @Html.DropDownListFor(x => x.GenreId, new SelectList(Model.Genres, "GenreId", "Name"))
</p>

<p> 
    @Html.LabelFor(model => model.ArtistId) 
    @Html.DropDownListFor(x => x.ArtistId, new SelectList(Model.Artists, "ArtistId", "Name"))
</p>

I would recommend you working with view models and avoid using any ViewBag. So you start by defining a view model:

public class AlbumViewModel
{
    public string GenreId { get; set; }
    public IEnumerable<Genre> Genres { get; set; }

    public string ArtistId { get; set; }
    public IEnumerable<Artist> Artists { get; set; }

    public Album Album { get; set; }
}

and then inside your controller action you would populate this view model:

public ActionResult Edit(int id)
{
    var model = new AlbumViewModel
    {
        Genres = storeDB.Genres.OrderBy(g => g.Name),
        Artists = storeDB.Artists.OrderBy(a => a.Name),
        Album = storeDB.Albums.Single(a => a.AlbumId == id)
    };
    return View(model);
}

and finally in your editor template (~/Views/Shared/EditorTemplates/AlbumViewModel.cshtml):

@model MvcMusicStore.Models.AlbumViewModel
<p> 
    @Html.LabelFor(model => model.GenreId) 
    @Html.DropDownListFor(x => x.GenreId, new SelectList(Model.Genres, "GenreId", "Name"))
</p>

<p> 
    @Html.LabelFor(model => model.ArtistId) 
    @Html.DropDownListFor(x => x.ArtistId, new SelectList(Model.Artists, "ArtistId", "Name"))
</p>
丶情人眼里出诗心の 2024-10-22 03:23:08

好的,我找到了问题,我从 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...

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