Solucionado (ver solução)
Solucionado
(ver solução)
6
respostas

Capitulo 6 ASP.NET MVC 5

Estou usando o Visual Studio Ultimate 2013 Update 3. No capitulo 6, não consigo fazer ele ser fortemente tipado e apresenta o seguinte erro:

Erro de Servidor no Aplicativo '/'.

Value cannot be null or empty.
Nome do parâmetro: linkText

Descrição: Ocorreu uma exceção sem tratamento durante a execução da atual solicitação da Web. Examine o rastreamento de pilha para obter mais informações sobre o erro e onde foi originado no código. 

Detalhes da Exceção: System.ArgumentException: Value cannot be null or empty.
Nome do parâmetro: linkText

Erro de Origem: 


Linha 27:                     <tr>
Linha 28:                         <td>@produto.Id</td>
Linha 29:                         <td>@Html.ActionLink(produto.Nome, "Visualiza", new { id = produto.Id })</td>
Linha 30:                         <td>@produto.Quantidade</td>
Linha 31:                     </tr>

Arquivo de Origem: g:\CaelumEstoque\CaelumEstoque\CaelumEstoque\Views\Produto\Index.cshtml    Linha: 29 

Rastreamento de Pilha: 


[ArgumentException: Value cannot be null or empty.
Nome do parâmetro: linkText]
   System.Web.Mvc.Html.LinkExtensions.ActionLink(HtmlHelper htmlHelper, String linkText, String actionName, String controllerName, RouteValueDictionary routeValues, IDictionary`2 htmlAttributes) +117
   System.Web.Mvc.Html.LinkExtensions.ActionLink(HtmlHelper htmlHelper, String linkText, String actionName, Object routeValues) +104
   ASP._Page_Views_Produto_Index_cshtml.Execute() in g:\CaelumEstoque\CaelumEstoque\CaelumEstoque\Views\Produto\Index.cshtml:29
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +272
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +121
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +131
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +695
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +382
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +431
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +116
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +529
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +106
   System.Web.Mvc.Async.<>c__DisplayClass28.<BeginInvokeAction>b__19() +321
   System.Web.Mvc.Async.<>c__DisplayClass1e.<BeginInvokeAction>b__1b(IAsyncResult asyncResult) +185
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(IAsyncResult asyncResult, ProcessRequestState innerState) +39
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9514928
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
6 respostas

Bom dia,

Você tipou ele no início da View?

Ex: @model IEnumerable

e no controller

namespace nome soluion.Controllers { public class NomeCController : Controller

private Contexto db = new Contexto(); // seu entity

public ActionResult Index() { var model = new ProdutoModel(); model.Produto= db.Produto.ToList();

return View(model); }

Atenciosamente,

Naiade Lima

Olá Naiade, obrigado pela resposta! Sim, fiz os passos mencionados. Veja como ficou meu ProdutoController.cs final:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CaelumEstoque.DAO;
using CaelumEstoque.Models;

namespace CaelumEstoque.Controllers
{
    public class ProdutoController : Controller
    {
        // GET: Produto
        public ActionResult Index()
        {
            ProdutosDAO dao = new ProdutosDAO();
            IList<Produto> produtos = dao.Lista();
            return View(produtos);
        }
        public ActionResult Form()
        {
            CategoriasDAO categoriasDAO = new CategoriasDAO();
            IList<CategoriaDoProduto> categorias = categoriasDAO.Lista();
            ViewBag.Categorias = categorias;
            ViewBag.Produto = new Produto();
            return View();
        }
        [HttpPost]
        public ActionResult Adiciona(Produto produto)
        {
            int idDataInformatica = 1;
            if (produto.CategoriaId.Equals(idDataInformatica) && produto.Preco < 100)
            {
                ModelState.AddModelError("produto.Invalido", "Informatica com preço abaixo de 100 reais.");
            }
            if (ModelState.IsValid)
            {
                ProdutosDAO dao = new ProdutosDAO();
                dao.Adiciona(produto);
                return RedirectToAction("Index");
            }
            else
            {
                ViewBag.Produto = produto;
                CategoriasDAO categoriasDAO = new CategoriasDAO();
                ViewBag.Categorias = categoriasDAO.Lista();
                return View("Form");
            }
        }
        public ActionResult Visualiza(int id)
        {
            ProdutosDAO dao = new ProdutosDAO();
            Produto produto = dao.BuscaPorId(id);
            return View(produto);
        }
    }
}

E este é meu Index.cshtml do Produto:

@model IList<CaelumEstoque.Models.Produto>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Produto</th>
                    <th>Quantidade</th>
                </tr>
            </thead>
            <tbody>
                @foreach (CaelumEstoque.Models.Produto produtos in Model)
                {
                    <tr>
                        <td>@produtos.Id</td>
                        <td>@Html.ActionLink(produtos.Nome, "Visualiza", new { id = produtos.Id })</td>
                        <td>@produtos.Quantidade</td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</body>
</html>

Também tentei assim o Index.cshtml:

@model IList<CaelumEstoque.Models.Produto>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Produto</th>
                    <th>Quantidade</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var produtos in Model)
                {
                    <tr>
                        <td>@produtos.Id</td>
                        <td>@Html.ActionLink(produtos.Nome, "Visualiza", new { id = produtos.Id })</td>
                        <td>@produtos.Quantidade</td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</body>
</html>

Sem sucesso

solução!

Era um problema com meu Visual Studio, reinstalei em outra máquina sem os plugins que davam conflito com o System.Web e funcionou.

Puts o meu esta dando o mesmo problema, mas não consegui resolver ainda, como faço Italo ?

Já descobri, tinha uma linha nula no meu banco, e na hora de gerar o link dava erro pois não tinha nome o produto. Valeu...

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software