Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Alex Hedlund 6 posts 55 karma points
    Jan 09, 2015 @ 02:33
    Alex Hedlund
    0

    Reversed RenderModel Dependancy Issue

    Hi all,

    I was able to break the dependancy on RenderModel in my MVC pages, but for some reason my web pages think I'm passing RenderModels to my views. I've followed the documentation on http://our.umbraco.org/documentation/reference/mvc/custom-controllers, and various guides. But I'm still getting the error "The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel', but this dictionary requires a model item of type 'TestSurfaceController.UserModel'."

    Layout currently inherits from

    Umbraco.Web.Mvc.UmbracoViewPage<dynamic>

    Here's my template:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<TestSurfaceController.UserModel>
    @{
    Layout = "umbLayout.cshtml";
    }
    @Html.Action("Index", "UserRegistration")

    The model:

    namespace TestSurfaceController
    {
    public class UserModel
    {
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }
    [Required]
    [EmailAddress]
    public string EmailAddress { get; set; }
    [Required]
    public string Username { get; set; }
    [Required]
    public string Password { get; set; }
    [Compare("Password")]
    public string ConfirmPassword { get; set; }
    }
    }

    The view:

    @model TestSurfaceController.UserModel

    @using(Html.BeginUmbracoForm("HandleFormSubmit"))
    {
    *the html that makes my form*
    }

    And the the important bit of the controller:

    namespace TestSurfaceController
    {
    public class UserRegistrationController : SurfaceController
    {
    /*
    * This action returns a fresh index view of the User Registration page.
    */
    public ActionResult Index()
    {
    return PartialView("UserRegistration", new UserModel());
    }
    ... more code

    As you can see, the UserModel doesn't inherit from RenderModel, and it's the model being passed to the view.

    I'm currently running Umbraco 7.2.0. Any help would be greatly appreciated!

  • Alex Hedlund 6 posts 55 karma points
    Jan 12, 2015 @ 18:07
    Alex Hedlund
    0

    Hello again,

    I updated my MVC to clear up any specific routing issues, but the problem is still occuring. I'm still running Umbraco 7.2.0.

    My document type is now named and aliased UserRegistration.

    The Layout inherits from:

    Umbraco.Web.Mvc.UmbracoViewPage<dynamic>

    The new UserRegistration template:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<TestSurfaceController.UserRegistrationModel>

    @{
    Layout = "umbLayout.cshtml";
    }

    @Html.Action("Index","UserRegistration", new TestSurfaceController.UserRegistrationModel());

    The updated model:

    namespace TestSurfaceController
    {
    public class UserRegistrationModel
    {
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }
    [Required]
    [EmailAddress]
    public string EmailAddress { get; set; }
    [Required]
    public string Username { get; set; }
    [Required]
    public string Password { get; set; }
    [Compare("Password")]
    public string ConfirmPassword { get; set; }
    }
    }

    The updated view:

    @model TestSurfaceController.UserRegistrationModel

    @using(Html.BeginUmbracoForm<TestSurfaceController.UserRegistrationController>("HandleFormSubmit"))
    {
    // html to render form.
    }

    And finally, the updated controller:

    namespace TestSurfaceController
    {
    public class UserRegistrationController : SurfaceController
    {
    public ActionResult Index(UserRegistrationModel model)
    {
    return PartialView("UserRegistration", model);
    }

    [HttpPost]
    public async Task<ActionResult> HandleFormSubmit(UserRegistrationModel model)
    {
    return PartialView("UserRegistration", model);
    }
    }

    If possible, I'd like to avoid extending RenderModel with my models because it seems a little limiting. Any help would be greatly appreciated!

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 13, 2015 @ 12:33
    Jeroen Breuer
    0

    Hello,

    The SurfaceController is for posting. If you want to use route hijacking you need to use RenderMvcController or add an interface to your SurfaceController. You can find an example in the Hybrid Framework

    Jeroen

  • Alex Hedlund 6 posts 55 karma points
    Jan 13, 2015 @ 19:17
    Alex Hedlund
    0

    Thank you for the reply! Unfortunately, extending RenderMvcController didn't solve the problem. Strangely enough, replacing the inherits statement in the template with

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    makes everything work. Do you have any idea why this happens?

Please Sign in or register to post replies

Write your reply to:

Draft