简体   繁体   中英

What needs to be in place in order to have a pagedList return to a post action?

I'm not gonna post up aload of code (again) to try and figure this out. This is an asp.net MVC 3 application using razor views. I just want to know what exactly needs to be in place to allow for a view using..

  @model IPagedList<Model>

To Post back that Model as a parameter to the Post Action. I have already tried a parameterless constructor in my model

   public Model model { get; set; }

and this aswell..

  public PagedList<Model> pagedList { get; set; }

and I still get this error.. "No parameterless constructor defined for this object"

I thought my Post action should look like this ...

   public ActionResult Index(IPrincipal user, PagedList<Model> model)

But it throws the error at this point. Anybody ?, I have looked at dozens of questions on OS but none seem to have a solution that would fit my project.

*****Edit*** Just to highlight, I did try adding this parameterless constructor, but error remains..

   public TrackerModel() { }

Just to give more details, this viewModel, TrackerModel, is used in a partial view in the Index view. So the Get Index ActionResult doesn't use the ViewModel directly but the partial view contained in its view does. It is referenced from the controller...

  public PartialViewResult AllCalibrations(int? page, IPrincipal user)

The viewModel is mapped from an entity class called "Calibration" via Automapper.

    Mapper.CreateMap<Calibration, TrackerModel>().ConvertUsing(new CalibrationToTrackerModel());

The calibrations Model, contains all the virtual properties used in the Database.

Your Model class should resemble something like this:

public class Model
{
     //some properties ...

     // parameterless constructor
     public Model() { }

     //constructor with params
     public Model(string id)
     {
     }
 }

The thing is that default model binder needs a constructor without params to instantiate your model. You can replace it by custom model binder if you have some good reasons for this (not needed in your case). If adding the constructor still does not help, let us know - then the issue is somewhere else also. But having parameterless constructor along with others is a first essential step here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM