Não consigo gerar DropDownList no HTML
05/08/2010
0
Estou trabalhando uma aplicação MVC 2 com Entity FrameWork para estudos, pois estou começando.
O problema ocorre quando tento gerar uma página com o dados vindo do BD, porém há dois campos que são listas, para o preenchimento de um DropDownList. Dois no caso. Os campos são gerados porém somente com TextBox.
Uso VS2010 Ultimate.
O que poderia estar errado? Segue o código!!
StoreManagerViewModel.cs. On the folder ViewModels
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcMusicStore.Models; namespace MvcMusicStore.ViewModel
{
public class StoreManagerViewModel
{
public Album Album { get; set; }
public List<Artist> Artists { get; set; }
public List<Genre> Genres { get; set; }
}
} StoreManagerController.cs. On the folder Controllers public ActionResult Edit(int id)
{
var viewModel = new StoreManagerViewModel()
{
Album = storeDB.Albums.Single(a => a.AlbumId == id),
Genres = storeDB.Genres.ToList(),
Artists = storeDB.Artists.ToList()
}; return View(viewModel);
}
Edit.aspx. On the folder ~\Views\StoreManager <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcMusicStore.ViewModel.StoreManagerViewModel>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Edit -
<%: Model.Album.Title %>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<form id="form1" runat="server">
<h2>
[ Edit ]
</h2>
<% using (Html.BeginForm())
{%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Edit Album</legend>
<%: Html.EditorFor(model => model.Album, new { Artists = Model.Artists, Genres = Model.Genres })%>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
</form>
</asp:Content>
Album.ascx. On the folder ~\Views\Shared\EditorTemplate
<%@ Import Namespace="MvcMusicStore" %>
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcMusicStore.Models.Album>" %>
<p>
<%: Html.LabelFor(model => model.Title)%>
<%: Html.TextBoxFor(model => model.Title)%>
<%: Html.ValidationMessageFor(model => model.Title)%>
</p>
<p>
<%: Html.LabelFor(model => model.Price)%>
<%: Html.TextBoxFor(model => model.Price)%>
<%: Html.ValidationMessageFor(model => model.Price)%>
</p>
<p>
<%: Html.LabelFor(model => model.AlbumArtUrl)%>
<%: Html.TextBoxFor(model => model.AlbumArtUrl)%>
<%: Html.ValidationMessageFor(model => model.AlbumArtUrl)%>
</p>
<p>
<%: Html.LabelFor(model => model.Artist)%>
<%: Html.DropDownList("ArtistId", new SelectList(ViewData["Artists"] as IEnumerable, "ArtistId", "Name", Model.ArtistId))%>
</p>
<p>
<%: Html.LabelFor(model => model.Genre)%>
<%: Html.DropDownList("GenreId", new SelectList(ViewData["Genres"] as IEnumerable, "GenreId", "Name", Model.GenreId))%>
</p>
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcMusicStore.Models; namespace MvcMusicStore.ViewModel
{
public class StoreManagerViewModel
{
public Album Album { get; set; }
public List<Artist> Artists { get; set; }
public List<Genre> Genres { get; set; }
}
} StoreManagerController.cs. On the folder Controllers public ActionResult Edit(int id)
{
var viewModel = new StoreManagerViewModel()
{
Album = storeDB.Albums.Single(a => a.AlbumId == id),
Genres = storeDB.Genres.ToList(),
Artists = storeDB.Artists.ToList()
}; return View(viewModel);
}
Edit.aspx. On the folder ~\Views\StoreManager <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcMusicStore.ViewModel.StoreManagerViewModel>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Edit -
<%: Model.Album.Title %>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<form id="form1" runat="server">
<h2>
[ Edit ]
</h2>
<% using (Html.BeginForm())
{%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Edit Album</legend>
<%: Html.EditorFor(model => model.Album, new { Artists = Model.Artists, Genres = Model.Genres })%>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
</form>
</asp:Content>
Album.ascx. On the folder ~\Views\Shared\EditorTemplate
<%@ Import Namespace="MvcMusicStore" %>
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcMusicStore.Models.Album>" %>
<p>
<%: Html.LabelFor(model => model.Title)%>
<%: Html.TextBoxFor(model => model.Title)%>
<%: Html.ValidationMessageFor(model => model.Title)%>
</p>
<p>
<%: Html.LabelFor(model => model.Price)%>
<%: Html.TextBoxFor(model => model.Price)%>
<%: Html.ValidationMessageFor(model => model.Price)%>
</p>
<p>
<%: Html.LabelFor(model => model.AlbumArtUrl)%>
<%: Html.TextBoxFor(model => model.AlbumArtUrl)%>
<%: Html.ValidationMessageFor(model => model.AlbumArtUrl)%>
</p>
<p>
<%: Html.LabelFor(model => model.Artist)%>
<%: Html.DropDownList("ArtistId", new SelectList(ViewData["Artists"] as IEnumerable, "ArtistId", "Name", Model.ArtistId))%>
</p>
<p>
<%: Html.LabelFor(model => model.Genre)%>
<%: Html.DropDownList("GenreId", new SelectList(ViewData["Genres"] as IEnumerable, "GenreId", "Name", Model.GenreId))%>
</p>
Fabio Colli
Curtir tópico
+ 0
Responder
Posts
05/08/2010
Fabio Colli
O que esqueci de falar é que a página Album.ascx não é acessada em nenhum momento. Porém eu não sei o porque.
Obrigado!!
Responder
05/08/2010
Fabio Colli
Caramba que coisa!!!
Eu só alterei o nome da pasta ~\Views\Shared\EditorTemplate para ~\Views\Shared\EditorTemplates e funcinou.
Agora o que isso teria de influência?
Responder
Clique aqui para fazer login e interagir na Comunidade :)