Problemas com MVC3. Meu projeto não funciona.
02/03/2012
0
O Model
namespace AppCapitulo4.Models { public class CategoriesRepository { public List<Categorie> GetAllCategories() { ConnectionStringSettings getString = WebConfigurationManager.ConnectionStrings[nwind] as ConnectionStringSettings; if (getString != null) { string sSql = select CategoryID,CategoryName, Description from Categories; using (SqlConnection conn = new SqlConnection(getString.ConnectionString)) { List<Categorie> lst = new List<Categorie>(); SqlDataReader dr = null; conn.Open(); SqlCommand cmd = new SqlCommand(sSql, conn); dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); if (dr.HasRows) { int categoryID = dr.GetOrdinal(CategoryID); int categoryName = dr.GetOrdinal(CategoryName); int description = dr.GetOrdinal(Description); while (dr.Read()) { Categorie p = new Categorie(); p.CategoryID = dr.GetInt32(categoryID); p.CategoryName = dr.GetString(categoryName); p.Description = dr.GetString(description).ToString(); } } return lst; } } return null; } public class Categorie { public int CategoryID { get; set; } public string CategoryName { get; set; } public string Description { get; set; } } } }
O View
<asp:Content ID=Content1 ContentPlaceHolderID=TitleContent runat=server> Index </asp:Content> <asp:Content ID=Content2 ContentPlaceHolderID=MainContent runat=server> <h2>Index</h2> <p> <%: Html.ActionLink(Create New, Create) %> </p> <table> <tr> <th></th> <th> CategoryID </th> <th> CategoryName </th> <th> Description </th> </tr> <% foreach (var item in Model) { %> <tr> <td> <%: Html.ActionLink(Edit, Edit, new { id=item.CategoryID }) %> | <%: Html.ActionLink(Details, Details, new { id = item.CategoryID })%> | <%: Html.ActionLink(Delete, Delete, new { id = item.CategoryID })%> </td> <td> <%: item.CategoryID %> </td> <td> <%: item.CategoryName %> </td> <td> <%: item.Description %> </td> </tr> <% } %> </table> </asp:Content>
O Controller
namespace AppCapitulo4.Controllers { public class CategorieController : Controller { CategoriesRepository _db = new CategoriesRepository(); public ActionResult Index() { var model = _db.GetAllCategories(); if (model == null) return View(NotFound); else return View(model); } } }
O Global.asax
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute(.axd/{*pathInfo}); routes.MapRoute( Default, // Route name //, // URL with parameters new { controller = Categorie, action = Index, id = UrlParameter.Optional } // Parameter defaults ); }
É mais ou menos isso.
Pjava
Posts
05/03/2012
Rodrigo Odasaki
Está impossível de ajudar.
06/03/2012
Pjava
namespace AppCapitulo4.Models
{
public class CategoriesRepository
{
public List<Categorie> GetAllCategories() {
ConnectionStringSettings getString = WebConfigurationManager.ConnectionStrings[nwind] as ConnectionStringSettings;
if (getString != null) {
string sSql = select CategoryID,CategoryName, Description from Categories;
using (SqlConnection conn = new SqlConnection(getString.ConnectionString)) {
List<Categorie> lst = new List<Categorie>();
SqlDataReader dr = null;
conn.Open();
SqlCommand cmd = new SqlCommand(sSql, conn);
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (dr.HasRows) {
int categoryID = dr.GetOrdinal(CategoryID);
int categoryName = dr.GetOrdinal(CategoryName);
int description = dr.GetOrdinal(Description);
while (dr.Read()) {
Categorie p = new Categorie();
p.CategoryID = dr.GetInt32(categoryID);
p.CategoryName = dr.GetString(categoryName);
p.Description = dr.GetString(description).ToString();
}
}
return lst;
}
}
return null;
}
public class Categorie {
public int CategoryID { get; set; }
public string CategoryName { get; set; }
public string Description { get; set; }
}
}
}
O View
<%@ Page Title= Language=C# MasterPageFile=~/Views/Shared/Site.Master Inherits=System.Web.Mvc.ViewPage<IEnumerable<AppCapitulo4.Models.CategoriesRepository+Categorie>> %>
<asp:Content ID=Content1 ContentPlaceHolderID=TitleContent runat=server>
Index
</asp:Content>
<asp:Content ID=Content2 ContentPlaceHolderID=MainContent runat=server>
<h2>Index</h2>
<p>
<%: Html.ActionLink(Create New, Create) %>
</p>
<table>
<tr>
<th></th>
<th>
CategoryID
</th>
<th>
CategoryName
</th>
<th>
Description
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%: Html.ActionLink(Edit, Edit, new { id=item.CategoryID }) %> |
<%: Html.ActionLink(Details, Details, new { id = item.CategoryID })%> |
<%: Html.ActionLink(Delete, Delete, new { id = item.CategoryID })%>
</td>
<td>
<%: item.CategoryID %>
</td>
<td>
<%: item.CategoryName %>
</td>
<td>
<%: item.Description %>
</td>
</tr>
<% } %>
</table>
</asp:Content>
O Controller
namespace AppCapitulo4.Controllers
{
public class CategorieController : Controller
{
CategoriesRepository _db = new CategoriesRepository();
public ActionResult Index()
{
var model = _db.GetAllCategories();
if (model == null)
return View(NotFound);
else
return View(model);
}
}
}
O Global ASAX
namespace AppCapitulo4
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute(.axd/{*pathInfo});
routes.MapRoute(
Default, // Route name
//, // URL with parameters
new { controller = Categorie, action = Index, id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}
}
O WEB Config
<connectionStrings>
<add name=nwind connectionString=Data Source=.\SQLExpress;Integrated Security=SSPI;Initial Catalog=northwind/>
</connectionStrings>
07/03/2012
Rodrigo Odasaki
Vamos por parte.
Por padrão no ASP.NET MVC ele já vem com uma rota padrão sendo:
Controler / Action / Id
A url dele: localhost/AppCapitulo4/Categorie
Então AppCapitulo4 deve ser a controler
E Categorie deve ser a action
Você comentou sobre o erro que recebe vindo do IIS, qual é o erro?
Alias, você está rodando o projeto direto por um servidor IIS, ou está rodando no Start Debugging do visual studio?
08/03/2012
Pjava
O erro estava em eu não colocar essa linha:
lst.add(p);
Clique aqui para fazer login e interagir na Comunidade :)