问题在ASP.NET MVC 3应用填充下拉框 [英] Problem populating dropdown boxes in an ASP.NET MVC 3 Application

查看:256
本文介绍了问题在ASP.NET MVC 3应用填充下拉框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了新的教程(musicstore)MVC的3对以上的 www.asp.net 。这一切都很好,除了在两个下拉框应填入从数据库的一部分 - 他们不是

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.

我也跟着教程和双重检查我的code。我认为这个问题可以用editorstemplate文件夹。不知道真的因为即时通讯新的MVC。所以,什么问题或如何调试呢?

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?

==============

==============

好了,所以这里是一些$ C $下album.cshtml这是在/视图/共享/ editortemplates /文件夹

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.cshtml在/views/storemanager/edit.cshtml,然后我也album.cshtml在/views/shared/editortemplates/album.cshtml。在下拉菜单都应该是从album.cshtml填充到edit.cshtml。我把code从album.cshtml直接进入edit.cshtml并能正常工作。所以我觉得问题是,editortemplates / album.cshtml不工作,即填充edit.cshtml页面。那么怎么办?谢谢...

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

==============

==============

好吧,我发现这个问题,我得到了工作源从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...

推荐答案

我会建议你用视图模型工作,避免使用任何 ViewBag 。所以,你首先定义视图模型:

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);
}

终于在编辑器模板(〜/查看/共享/ 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>

这篇关于问题在ASP.NET MVC 3应用填充下拉框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆