Skip to content

Instantly share code, notes, and snippets.

@InfinitiesLoop
Created August 29, 2012 05:06
Show Gist options
  • Save InfinitiesLoop/3507028 to your computer and use it in GitHub Desktop.
Save InfinitiesLoop/3507028 to your computer and use it in GitHub Desktop.
Error when trying to pass a model to a Child Action that contains an IList derived property
public interface IPagedList<T> : IList<T> {
int PageIndex { get; set; }
int TotalCount { get; set; }
int PageSize { get; set; }
}
public class SomeModel {
public int Foo { get; set; }
public IPagedList<int> Items { get; set; }
}
public class SomeController : Controller {
[ChildActionOnly]
public ActionResult Foo(SomeModel model) {
// significant stuff happens, using all kinds of dependencies and stuff
return View(model);
}
}
// a view.cshtml
// This call fails in model binding due to: Cannot create instance of Interface IPagedList
// Intent is to just pass this model directly to the action, model binding is unnecessary,
// but it seems you can only do that with Partials. Don't want a partial, there is significant
// logic happening inside Foo().
@Html.Action("Foo", "Some", new SomeModel {
Foo = 88,
// do not pass Items property
});
@bradwilson
Copy link

Change to:

@Html.Action("Foo", "Some", new { model = new SomeModel { ... } });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment