0
I am getting a null reference error and do not understand why. Follow the error:
System.NullReferenceException: 'Referência de objeto não definida para uma instância de um objeto.'
System.Web.Mvc.WebViewPage<TModel>.Model.get retornou null.
I debugged where I do the assignments of values for the variables where you are giving null reference.
Gave a Quick watch and I saw that you are receiving yes my values.
Follow the code from where the error occurs:
@model Application.ViewModels.ProdutoViewModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Produto</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Descricao, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Descricao, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Descricao, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Categoria, "Categoria", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
// acontece o erro nessa linha. Qualquer coisa coloquei o
// print do erro em um link lá em cima
@Html.DropDownListFor(model => model.CategoriaId,
new SelectList(Model.Categorias, "CategoriaId", "CategoriaNome"), new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CategoriaId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.SubCategoria, "SubCategoria", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.SubCategoriaId,
new SelectList(Model.SubCategorias, "SubCategoriaId", "SubCategoriaNome"), new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.SubCategoriaId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UnidadeMedida, "Unidade de Medida", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.UndMedidaId,
new SelectList(Model.UnidadeMedidas, "UndMedidaId", "UndMedidaNome"), new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.UndMedidaId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Criar" class="btn btn-primary" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Voltar", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Method of controller product
// GET: Produtos/Create
public ActionResult Create()
{
_produtoViewModel = new ProdutoViewModel();
_produtoViewModel.Categorias = _categoriaAppService.ObterTodas();
_produtoViewModel.SubCategorias = _subCategoriaAppService.ObterTodas();
_produtoViewModel.UnidadeMedidas = _undMedidaAppService.ObterTodas();
return View();
}
Stack
System.NullReferenceException ocorrido
HResult=0x80004003
Message=Referência de objeto não definida para uma instância de um objeto.
Source=App_Web_5yz4eaco
StackTrace:
em ASP._Page_Views_Produtos_Create_cshtml.Execute() em c:\Users\iagof\Source\Workspaces\Views\Produtos\Create.cshtml:linha 28
em System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
em System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
em System.Web.WebPages.StartPage.RunPage()
em System.Web.WebPages.StartPage.ExecutePageHierarchy()
em System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
em System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
em System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
em System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
em System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
em System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
em System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
Man, you’re trying to get a list of an object that hasn’t been passed to view. Wouldn’t it be better if you did a
ViewBag
with your lists and use in your dropdown?– L. Jhon