2009-11-13 3 views
0

"ListaDimensiones"속성을 제어하려는 이유를 이해하지 못하는 경우이 오류가 발생합니다.ASP.NET MVC - 컨트롤 모델의 속성을 사용하려고 할 때 오류가 발생했습니다.

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{} } 

     // 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> 

오류 : 개체 참조가 개체의 인스턴스로 설정되지 않았습니다.

답변

4

당신은 컨트롤러 액션 메소드에서보기로 뷰 모델 클래스의 인스턴스를 전달해야 : 당신은 당신의보기로 모델을 전달하지 않을

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

.

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

당신은 아무것도 뷰 모델 속성을 설정하지 않을 :

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

는 무언가 같이이어야한다.

컨트롤러에서 목록을 View 메서드로 전달합니다.

return View(new DimensionesViewModel()); 
1

DimensionesViewModel 클래스의 인스턴스를 뷰에 제공해야합니다. 이를 위해, 당신은 색인 작업이 같은 작업을 수행해야합니다

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

당신은 완전한 예를 here를 읽을 수 있습니다.

관련 문제