开发者

ASP.NET MVC - Error wish to use a property of a control model

I have this error when wanting to use the property "ListaDimensiones" in a controlled and do not understand why.

namespace Mkt.Web.Controllers.Cubo
{
    //
    // ViewModel Classes

    public class DimensionesViewModel
    {
        // Variables Properties
        IList<Dimension> _listaDimensiones = new List<Dimension>();

        // Properties
        public IList<Dimension> ListaDimensiones { get{return _listaDimensiones;} private set{} }

        //开发者_Go百科 Constructor
        public DimensionesViewModel()
        {
            _listaDimensiones = Dimensiones.GetListaFiltros(null);
        }

    }

    [HandleError]
    public class DimensionesController : Controller
    {
        //
        // GET: /Dimensiones/

        public ActionResult Index()
        {
            return View();
        }

    }
}


<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Mkt.Web.Controllers.Cubo.DimensionesViewModel>" %>
<%@ Import Namespace="Mkt.Web.Helpers" %>
<%@ Import Namespace="Mkt.Web.Models.Endidades" %>

<div>
    <!-- Error in this line for "Model.ListaDimensiones" --> 
    <%= Html.Table("myTable", (IList)Model.ListaDimensiones, null) %>  
</div>

Error: Object reference not set to an instance of an object.


You need to pass an instance of the ViewModel class to your view from the controller action method:

    public ActionResult Index()
    {
        var model = new DimensionesViewModel();
        return View(model);
    }


You're not passing in the model to your view.

    public ActionResult Index()
    {
        return View();
    }

Should be something like:

    public ActionResult Index()
    {
        var model = new DimensionesViewModel();
        return View(model);
    }


You're not setting the Views Model property to anything.

In your controller passing your list to the View method.

return View(new DimensionesViewModel());


You need to give to your view an instance of the class DimensionesViewModel. For that, you need to do something like this in the Index action:

public ActionResult Index()
{
  return View(new DimensionesViewModel());
}

You can read a complete example here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜