Copied to clipboard

Flag this post as spam?

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


  • Luke Alderton 192 posts 509 karma points
    Dec 18, 2012 @ 11:38
    Luke Alderton
    0

    MVC Using a custom controller and viewmodel

    I'm writing a website in Umbraco 4.11.0 using MVC and I'm trying to add a form to a page, so far I've learnt almost everything from the MVC documentation on this page: http://our.umbraco.org/documentation/Reference/Mvc.

    To create the form I've assumed that I need to use a custom controller in order to handle everything when submitting the form etc, I've used this page to help me in that respect: http://our.umbraco.org/documentation/Reference/Mvc/custom-controllers but I keep recieving an error when loading the page, the error is 

    The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel', but this dictionary requires a model item of type 'MyMvcApp.Models.StatusViewModel'.

    I'm using

    @model MyMvcApp.Models.StatusViewModel

    on the partial view and I've also tried using

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<MyMvcApp.Models.StatusViewModel>

    but it gives the same error!

    My Partial contains the following code:

    @model MyMvcApp.Models.StatusViewModel
    
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    
    @using (Html.BeginForm(new { enctyp = "multipart/form-data" }))
    {
        @Html.AntiForgeryToken("StatusViewForm")
        @Html.ValidationSummary(true)
    
        <fieldset>
            <legend>StatusViewModel</legend>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.StatusText)
            </div>
            <div class="editor-field">
                @Html.TextAreaFor(model => model.StatusText)
                @Html.ValidationMessageFor(model => model.StatusText)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Files)
            </div>
            <div class="editor-field">
                <input type="file" multiple name="Files" id="Files" />
                @Html.ValidationMessageFor(model => model.Files)
            </div>
    
            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
    }


    And my Controller:

    using MyMvcApp.Models;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace MyMvcApp.Controllers
    {
        public class StatusController : Umbraco.Web.Mvc.RenderMvcController
        {
            //
            // GET: /Status/
    
            [HttpGet]
            public PartialViewResult Index()
            {
                return new PartialViewResult();
            }
    
            [HttpPost]
            [ValidateAntiForgeryToken(Salt="StatusViewForm")]
            public PartialViewResult Index(StatusViewModel model)
            {
                if (ModelState.IsValid)
                {
    
                }
                return new PartialViewResult();
            }
        }
    }
    

    And my model:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Web;
    
    namespace MyMvcApp.Models
    {
        public class StatusViewModel
        {
            [Required]
            public string StatusText { get; set; }
            public IEnumerable<HttpPostedFileBase> Files { get; set; }
        }
    }
    I've put my partial into a folder called 'Status' within the 'Views' folder as from looking at the documentation it tells me that the controler requires it to be there?
    Thanks in advance for any help you can give me, much appretiated!
  • Wing 17 posts 39 karma points
    Jan 11, 2013 @ 11:30
    Wing
    0

    I've got the same error. It looks like the controller is not being hit at all. I've already made sure the controller derives from Umbraco.Web.Mvc.RenderMvcController

    Also included the dll from MVC containing the controller in the Umbraco Framework, but no luck.

  • Luke Alderton 192 posts 509 karma points
    Jan 11, 2013 @ 11:31
    Luke Alderton
    100

    I found out how to get it working and made a blog post about this topic here: http://lukealderton.co.uk/blog/blog/2012/december/mvc-forms-in-umbraco/

  • Wing 17 posts 39 karma points
    Jan 11, 2013 @ 13:11
    Wing
    0

    Thanks for sharing :)

  • Luke Alderton 192 posts 509 karma points
    Jan 11, 2013 @ 14:01
    Luke Alderton
    0

    No problem, hope it helped!

  • Adrian Inman 35 posts 59 karma points
    Jan 28, 2013 @ 13:35
    Adrian Inman
    0

    HI Luke, unless the problem is because I'm not using App_Code, but a separate compiled assembly, your solution on your page doesn't work for me as the model isn't the right type - Umbraco.Web.Models.RenderModel doesn't have the properties from the model and so exceptions are generated.

     

    Where version of Umbraco are you using?

  • Luke Alderton 192 posts 509 karma points
    Jan 28, 2013 @ 16:34
    Luke Alderton
    0

    Could I just ask if you've tried it in the app_code folder? If it works there then you have something wrong with your assembly. Also when you're callign the form, are you providing the model you want to use with it like this: `@Html.Partial("CommentForm", newLukeAlderton.CommentFormModel())`?

    Edit:
    I'm using Umbraco 4.11.x but it should work in 4.10.x and upwards.

     

     

     

     

     

     

     

  • Aron Lakatos 5 posts 25 karma points
    Mar 20, 2018 @ 16:49
  • Luke Alderton 192 posts 509 karma points
    Mar 20, 2018 @ 17:00
Please Sign in or register to post replies

Write your reply to:

Draft