tickwave
tickwave

Reputation: 3443

MVC Passing model property to renderpartial failed

So I have a model

public class test
{
    private List<int> _abc;

    public List<int> abc
    {
        get { return _abc; }
        set { _abc = value; }
    }
}

My partial page:

@model List<int>

//do something with model

My main page :

@model somenamespace.test

@Html.Partial("~/Views/Test/partial.cshtml", model.abc)

But I got this error when trying to access the page :

The model item passed into the dictionary is of type , but this dictionary requires a model item of type 'System.Collections.Generic.List`1[System.Int32]'.

I'm confused because my partial view accept list of integer and I pass property with list of integer as datatype, anything wrong with my code?

Any help will be appreciated and sorry for bad english.

Upvotes: 0

Views: 648

Answers (1)

user3559349
user3559349

Reputation:

Your property abc is null, and by default the test model is then passed to the partial, resulting in the error. Ensure you initialize abc in the controller, or in a parameterless constructor for test

For example

public class test
{
  public test()
  {
    abc = new List<int>();
  }
  public List<int> abc { get; set; }
}

Upvotes: 1

Related Questions