Created
August 29, 2012 05:06
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}); |
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
I don't see why the new IPagedList needs to be created -- none was passed in, why must there be an empty list? Regardless this whole model binding step is not necessary, I just want to pass the model directly into the child action. I'd settle for a different way of passing a model if there were such a way.